cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Have your say in shaping JMP's future by participating in the new JMP Wish List Prioritization Survey
Choose Language Hide Translation Bar
ts2
ts2
Level III

Sorting list of lists

JMP12

How to sort a list of lists by one list in the list of lists?

For example I want to sort by the numeric part of this list. 

I know I could iterate or make a table to sort this way, but is there a one-liner I don't know of? I feel like there must be an easier way to do it.

 

list = {{"a", "b", "c", "d"}, {2, 3, 4, 1}};

a,2

b,3

c,4

d,1

 

(list[1] and list[2] sorted by list[2])

{{"d", "a", "b", "c"}, {1, 2, 3, 4}};

d,1

a,2

b,3

c,4

1 ACCEPTED SOLUTION

Accepted Solutions
Craige_Hales
Super User

Re: Sorting list of lists

You probably want the rank function. See Sorting Lists .

 

list = {{"a", "b", "c", "d"}, {2, 3, 4, 1}};

r = rank(list[2]);
list[1] = list[1][r];
list[2] = list[2][r];

show(list);

list = {{"d", "a", "b", "c"}, {1, 2, 3, 4}};

 

rank() returns a vector of indexes that retrieve the elements in order.

 

Craige

View solution in original post

2 REPLIES 2
Craige_Hales
Super User

Re: Sorting list of lists

You probably want the rank function. See Sorting Lists .

 

list = {{"a", "b", "c", "d"}, {2, 3, 4, 1}};

r = rank(list[2]);
list[1] = list[1][r];
list[2] = list[2][r];

show(list);

list = {{"d", "a", "b", "c"}, {1, 2, 3, 4}};

 

rank() returns a vector of indexes that retrieve the elements in order.

 

Craige
ts2
ts2
Level III

Re: Sorting list of lists

Thanks this is exactly what I was looking for.