Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a list that I need to be able to assign ranks to individual elements in, giving it a number related to its position if the list were to be sorted, allowing for ties. For example, { 6, 3, 3, 6, 4, 4, 6, 4} would be transformed to { 1, 3, 3, 1, 2, 2, 1, 2} or { 3, 1, 1, 3, 2, 2, 3, 2} depending on if I were sorting for higher values or lower values.

This is for a variant on PageRank, in which after assigning each point or edge a value, the set of all edges or points is ranked, with the highest ranked point(s) getting the number 1, the second highest point(s) getting 2, and so on. I have tried using Sort and other approaches, but have not encountered anything that could be described as elegant.

share|improve this question

How about:

list /. Thread[# -> Ordering@#] &@Union@list
(* {3, 1, 1, 3, 2, 2, 3, 2} *)

You can use the additional arguments of Ordering to sort it the other way:

list /. Thread[# -> Ordering[#, All, Greater]] &@Union@list
(* {1, 3, 3, 1, 2, 2, 1, 2} *)
share|improve this answer

Your Answer


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.