Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

How can I sort a list and also get a list of indices that sort this list? For example: primary list is a

a={1,4,6,7,0,2};

sorted list is b

b={7,6,4,2,1,0}

and also return list c

c={4,3,2,6,1,5}
share|improve this question
add comment

1 Answer 1

up vote 9 down vote accepted

To get list b, you need Sort[]: Sort[{1, 4, 6, 7, 0, 2}, GreaterEqual].

To get list c, you need Ordering[]: Ordering[{1, 4, 6, 7, 0, 2}, All, GreaterEqual].

share|improve this answer
1  
+1, Beat me by 20 seconds :-) –  Heike Jun 24 '12 at 11:59
    
@Heike: I guess there is such a thing as being in the right place at the right time... :) –  J. M. Jun 24 '12 at 12:00
    
@J.M. : Thank you –  Vahid Iraji Jun 24 '12 at 12:03
5  
Also, if you need both, you might first do Ordering and then b=a[[c]] –  Rojo Jun 24 '12 at 14:00
    
@Rojo: that's a good point with respect to saving sorting effort. :) –  J. M. Jun 24 '12 at 14:04
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.