Elm Language Lists and Iteration Sorting a list in descending order


By default List.sort sorts in ascending order, with the compare function.

There are two ways to sort in descending order: one efficient and one inefficient.

  1. The efficient way: List.sortWith and a descending comparison function.
descending a b =
    case compare a b of
      LT -> GT
      EQ -> EQ
      GT -> LT

> List.sortWith descending [1,5,9,7,3]
[9,7,5,3,1] : List number
  1. The inefficient way (discouraged!): List.sort and then List.reverse.
> List.reverse (List.sort [1,5,9,7,3])
[9,7,5,3,1] : List number