TransWikia.com

Ordering a list of rules by its 4 last elements of LHS

Mathematica Asked by Hegel19 on April 26, 2021

I have a list of rules such as

listofrules = {{1, 1, 2, 5, 14} -> 1, {1, 1, 2, 7, 12} -> 1, {137, 1, 2, 6, 16} -> 
      1, {137, 1, 2, 8, 13} -> 1, {273, 1, 2, 3, 5} -> 
      1, {273, 1, 2, 3, 7} -> 2, {273, 1, 2, 9, 12} -> 
      1, {273, 1, 2, 9, 14} -> -2, {409, 1, 2, 3, 6} -> 
      1, {409, 1, 2, 3, 8} -> 
      2, {409, 1, 2, 9, 13} -> -1, {409, 1, 2, 9, 16} -> 
      2, {545, 1, 2, 4, 5} -> 1, {577,1,1,1,1} -> -3 ...... , {9520, 16, 16, 13, 16} -> 2, {9520, 16, 16, 1, 1} -> 
      2, {9520, 16, 16, 2, 2} -> -2, {9520, 16, 16, 5, 5} -> -4, {9520, 
       16, 16, 6, 6} -> 
      3, {9520, 16, 16, 7, 7} -> -12, {9520, 16, 16, 8, 8} -> -1, {9520, 
       16, 16, 10, 10} -> -1, {9520, 16, 16, 12, 12} -> -4, {9520, 16, 16,
        13, 13} -> 
      3, {9520, 16, 16, 14, 14} -> -12, {9520, 16, 16, 15, 15} -> 
      4, {9520, 16, 16, 16, 16} -> -1}

I want to sort it, ordering the last four elements of the LHS in growing order, irrespective of the first element.

For example, I want all the rules {#,1,1,1,1} -> # first, then all the rules {#,1,1,1,2} -> # etc…

How can I do that in an efficient way?

3 Answers

Try

listofrules={
  {1,1,2,5,14}->1,{1,1,2,7,12}->1,{137,1,2,6,16}->1,{137,1,2,8,13}->1,
  {273,1,2,3,5}->1,{273,1,2,3,7}->2,{273,1,2,9,12}->1,{273,1,2,9,14}->-2,
  {409,1,2,3,6}->1,{409,1,2,3,8}->2,{409,1,2,9,13}->-1,{409,1,2,9,16}->2,
  {545,1,2,4,5}->1,{577,1,1,1,1}->-3,{9520,16,16,13,16}->2,{9520,16,16,1,1}->2,
  {9520,16,16,2,2}->-2,{9520,16,16,5,5}->-4,{9520,16,16,6,6}->3,
  {9520,16,16,7,7}->-12,{9520,16,16,8,8}->-1,{9520,16,16,10,10}->-1,
  {9520,16,16,12,12}->-4,{9520,16,16,13,13}->3,{9520,16,16,14,14}->-12,
  {9520,16,16,15,15}->4,{9520,16,16,16,16}->-1};
SortBy[listofrules,#[[1,{2,3,4,5}]]&]

which extracts the items that you want to sort by and thus returns

{{577, 1, 1, 1, 1} -> -3, {273, 1, 2, 3, 5} -> 1, {409, 1, 2, 3, 6} -> 1, 
 {273, 1, 2, 3, 7} -> 2, {409, 1, 2, 3, 8} -> 2, {545, 1, 2, 4, 5} -> 1, 
 {1, 1, 2, 5, 14} -> 1, {137, 1, 2, 6, 16} -> 1, {1, 1, 2, 7, 12} -> 1, 
 {137, 1, 2, 8, 13} -> 1, {273, 1, 2, 9, 12} -> 1, {409, 1, 2, 9, 13} -> -1, 
 {273, 1, 2, 9, 14} -> -2, {409, 1, 2, 9, 16} -> 2, {9520, 16, 16, 1, 1} -> 2, 
 {9520, 16, 16, 2, 2} -> -2, {9520, 16, 16, 5, 5} -> -4, {9520, 16, 16, 6, 6} -> 3, 
 {9520, 16, 16, 7, 7} -> -12, {9520, 16, 16, 8, 8} -> -1, 
 {9520, 16, 16, 10, 10} -> -1, {9520, 16, 16, 12, 12} -> -4, 
 {9520, 16, 16, 13, 13} -> 3, {9520, 16, 16, 13, 16} -> 2, 
 {9520, 16, 16, 14, 14} -> -12, {9520, 16, 16, 15, 15} -> 4, 
 {9520, 16, 16, 16, 16} -> -1}

For this particular choice of the items to sort by

SortBy[listofrules,Rest[#[[1]]]&]

will also work. And there are probably several other ways of selecting the desired four elements.

Answered by Bill on April 26, 2021

#[[Ordering[#[[All, 1, 2 ;;]]]]] & @ listofrules 
{{577, 1, 1, 1, 1} -> -3, {273, 1, 2, 3, 5} -> 1, {409, 1, 2, 3, 6} -> 1, 
{273, 1, 2, 3, 7} ->  2, {409, 1, 2, 3, 8} -> 2, {545, 1, 2, 4, 5} -> 1, 
{1, 1, 2, 5, 14} -> 1, {137, 1, 2, 6, 16} -> 1, {1, 1, 2, 7, 12} -> 1, 
{137, 1, 2, 8, 13} -> 1, {273, 1, 2, 9, 12} -> 1, {409, 1, 2, 9, 13} -> -1,
{273, 1, 2, 9, 14} -> -2, {409, 1, 2, 9, 16} -> 2, {9520, 16, 16, 1, 1} -> 2, 
{9520, 16, 16, 2, 2} -> -2, {9520, 16, 16, 5, 5} -> -4, 
{9520, 16, 16, 6, 6} -> 3, {9520, 16, 16, 7, 7} -> -12, {9520, 16, 16, 8, 8} -> -1, 
{9520, 16, 16, 10, 10} -> -1, {9520, 16, 16, 12, 12} -> -4, 
{9520, 16, 16, 13, 13} -> 3, {9520, 16, 16, 13, 16} -> 2, 
{9520, 16, 16, 14, 14} -> -12, {9520, 16, 16, 15, 15} -> 4, 
{9520, 16, 16, 16, 16} -> -1}

This should be faster than SortBy for long lists.

Answered by kglr on April 26, 2021

Another possibility using operator forms (and avoiding pure functions) is:

SortBy[listofrules, Rest @* First]

{{577, 1, 1, 1, 1} -> -3, {273, 1, 2, 3, 5} -> 1, {409, 1, 2, 3, 6} -> 1, {273, 1, 2, 3, 7} -> 2, {409, 1, 2, 3, 8} -> 2, {545, 1, 2, 4, 5} -> 1, {1, 1, 2, 5, 14} -> 1, {137, 1, 2, 6, 16} -> 1, {1, 1, 2, 7, 12} -> 1, {137, 1, 2, 8, 13} -> 1, {273, 1, 2, 9, 12} -> 1, {409, 1, 2, 9, 13} -> -1, {273, 1, 2, 9, 14} -> -2, {409, 1, 2, 9, 16} -> 2, {9520, 16, 16, 1, 1} -> 2, {9520, 16, 16, 2, 2} -> -2, {9520, 16, 16, 5, 5} -> -4, {9520, 16, 16, 6, 6} -> 3, {9520, 16, 16, 7, 7} -> -12, {9520, 16, 16, 8, 8} -> -1, {9520, 16, 16, 10, 10} -> -1, {9520, 16, 16, 12, 12} -> -4, {9520, 16, 16, 13, 13} -> 3, {9520, 16, 16, 13, 16} -> 2, {9520, 16, 16, 14, 14} -> -12, {9520, 16, 16, 15, 15} -> 4, {9520, 16, 16, 16, 16} -> -1}

Answered by Carl Woll on April 26, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP