Sort values. You can use this as a self-documenting alternative to List.sort:
Sort.list Sort.alphabetical [ "foo", "bar", "baz" ]
--> [ "baz", "bar", "foo" ]You can also use Sort.by and Sort.reverse to transform sorters:
-- Sort users by name, in reverse alphabetical order
Sort.list
(Sort.by .name (Sort.reverse Sort.alphabetical))
usersFinally, you can use these to create sets and dictionaries with keys that
do not have to be comparable. See the Sort.Dict and Sort.Set modules.
Reverse a sorter.
Sort.list Sort.alphabetical [ "b", "c", "a" ]
--> [ "a", "b", "c" ]
Sort.list (Sort.reverse Sort.alphabetical) [ "b", "c", "a" ]
--> [ "c", "b", "a" ]Sort by a derived value.
One way to use this is with collections of records:
Sort.list (Sort.by .name String.alphabetical)
[ { name = "Bo" }, { name = "Amy" }, { name = "Cam" } ]
--> [ { name = "Amy" }, { name = "Bo" }, { name = "Cam" } ]Another use is for unwrapping union type constructors:
import Sort exposing (Sorter)
type Username
= Username String
alphabetical : Sorter Username
alphabetical =
Sort.by (\(Username str) -> str) Sort.alphabeticalSort strings alphabetically.
Sort.list Sort.alphabetical [ "cabbage", "banana", "apple" ]
--> [ "apple", "banana", "cabbage" ]To sort in reverse alphabetical order, use Sort.reverse
Sort.list (Sort.reverse Sort.alphabetical) [ "cabbage", "banana", "apple" ]
--> [ "cabbage", "banana", "apple" ]Sort numbers from lowest to highest.
Sort.list Sort.increasing [ 4, 7, 2 ]
--> [ 2, 4, 7 ]To sort decreasing, use Sort.reverse
Sort.list (Sort.reverse Sort.increasing) [ 4, 7, 2 ]
--> [ 7, 4, 2 ]Sort a list using a Sorter.
This can be used as an alternative to List.sort, List.sortBy, and List.sortWith.
Sort.list Sort.alphabetical [ "foo", "bar", "baz" ]
--> [ "baz", "bar", "foo" ]You can also use Sort.by and Sort.reverse to transform sorters:
-- Sort users by name, in reverse alphabetical order
Sort.list
(Sort.by .name (Sort.reverse Sort.alphabetical))
users
A description of how to sort values of a particular type.