1
$\begingroup$

I already know the sorted positions, so I want to sort based on that.

Say, I have list of {1, 3}, I want the sorted position to be {2, 1}, as 2 is the second element, 3 and 1 is the first element 1.

How should I write the sort function to be most simple.

Should I write something like Sort[{1, 3}, {2, 1}{{_}}] where _ should represent the indice of that element in the original list.

$\endgroup$
1
  • 6
    $\begingroup$ {1,3}[[{2,1}]]? More generally, list[[Ordering[list]]] $\endgroup$
    – kglr
    Apr 27, 2017 at 17:43

1 Answer 1

4
$\begingroup$
{1,3}[[{2,1}]]

{3, 1}

Generally,

list = {1, 3};
positions = {2,1};
list[[positions]]

{3, 1}

$\endgroup$

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