5
$\begingroup$

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}
$\endgroup$
9
$\begingroup$

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].

$\endgroup$
  • $\begingroup$ @Heike: I guess there is such a thing as being in the right place at the right time... :) $\endgroup$ – J. M. will be back soon Jun 24 '12 at 12:00
  • $\begingroup$ @J.M. : Thank you $\endgroup$ – Vahid Iraji Jun 24 '12 at 12:03
  • 5
    $\begingroup$ Also, if you need both, you might first do Ordering and then b=a[[c]] $\endgroup$ – Rojo Jun 24 '12 at 14:00
  • $\begingroup$ @Rojo: that's a good point with respect to saving sorting effort. :) $\endgroup$ – J. M. will be back soon Jun 24 '12 at 14:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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