SeedRandom[417]
list1 = RandomChoice[Range[22, 28], 4]
{22, 24, 26, 23}
We can get the rank list
Position[Sort[list1], #] & /@ list1 // Flatten
{1, 3, 4, 2}
But when the list have a duplicated element,this method doesn't work,Such as
SeedRandom[416]
list2=RandomChoice[Range[22,28],4]
{28, 24, 25, 24}
A list {3,1,2,1} be expected,Any advice can be gived?
Ordering@Ordering[list]? – Apr 14 '16 at 16:16list1.Butlist2? – yode Apr 14 '16 at 16:18position[list_List] := Module[{n = 1, f}, f[x_] := f[x] = n++; f /@ Sort[list]; f /@ list]? – Apr 14 '16 at 16:22Position[Union[list2],#]&/@list2//Flatten– yode Apr 14 '16 at 16:26positionfor large lists. – Apr 14 '16 at 16:32performance-tuningtag if you are indeed interested in this aspect. If you do so, please do not accept my answer right away, others may come with faster solutions. – Apr 14 '16 at 16:38# /. Thread[(# -> Ordering@Ordering@#) & @ DeleteDuplicates@#] & /@ {list1, list2}gives {{1, 3, 4, 2}, {3, 1, 2, 1}} – user1066 Apr 16 '16 at 10:29