Suppose ratings are already sorted or not?
Here's how to solve it if they cannot be considered sorted:
Prelude Data.List Data.Function> let f = (const .) . (,) :: a -> b -> c -> (a,b) Prelude Data.List Data.Function> let sor s = sortBy ((flip compare) `on` snd) s Prelude Data.List Data.Function> let rank scores = map fst . sor . concat . zipWith (map . uncurry . f) [1..] . groupBy ((==) `on` snd) . sor . zip [1,0..] $ scores Prelude Data.List Data.Function> rank [11,13,13,12] [3,1,1,2]
First, attach a reverse index to each account. Then sort in descending order by the second projection of the tuple. Grouping by the second projection gives us a list of lists of points, which should have the same rank. Zipping with a list of ranks assigns rank 1 to the largest number. Using the function f , we at the same time discard the actual score by making tuples a pair (rank, reverse index). Now you need to sort these tuples in descending order of the inverse index and get the first projection - rank.
Sassa nf
source share