# Generating a new list according to the values of the elements of a list

I want to use the command Permute to arrange the rows of a matrix. What I am looking to do is given a list I want to generate a new list which gives me the positions of the elements of the first list according to their values. That is, if there is a list $\{p_1,p_2,p_3,p_4\}$ such that $p_2>p_3>p_4>p_1$ then I would want a list $\{4,1,2,3\}$. For example: I have a list $\{2,1,3,0\}$ which I want to convert into $\{2,3,1,4\}$ so that I could arrange the rows of the matrix according to the latter list.

• I do not understand what is "the positions of the elements of the first list according to their values". Commented Sep 20, 2018 at 8:13
• It means if there is a list $\{p1,p2,p3,p4\}$ such that $p2>p3>p4>p1$ then I would want a list $\{4,1,2,3\}$. @Szabolcs Commented Sep 20, 2018 at 8:18
• Maybe you are looking for Ordering: Reverse@Ordering[{2, 1, 3, 0}] is {3, 1, 2, 4}. Commented Sep 20, 2018 at 8:19

It seems that you want:

• Decreasing order, not increasing. We need Reverse.
• The inverse permutation of the result of Ordering. We need an extra Ordering to invert it.
a = {2, 1, 3, 0}
(* {2, 1, 3, 0} *)

result = Ordering@Reverse@Ordering[a]
(* {2, 3, 1, 4} *)


The question would have been much easier to understand if it is also stated in precise terms rather than just an example. For example, you could have said that you needed

ReverseSort[a][[result]] === a

• Thank you. It works. @Szabolcs Commented Sep 20, 2018 at 9:22