Sort values. You can use this as a self-documenting alternative to List.sort
:
Sort.list Sort.alphabetical [ "foo", "bar", "baz" ]
--> [ "bar", "baz", "foo" ]
You can also use Sort.by
, Sort.reverse
, and Sort.tiebreaker
to transform sorters:
-- Sort users by name, in reverse alphabetical order
Sort.list
(Sort.by .name (Sort.reverse Sort.alphabetical))
users
Finally, 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.
A description of how to sort values of a particular type.
custom : (a -> a -> Basics.Order) -> Sorter a
Create a custom Sorter
by defining how to order two values.
toOrder : Sorter a -> a -> a -> Basics.Order
Determine an Order for two values, based on the given
Sorter
reverse : Sorter a -> Sorter a
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" ]
by : (b -> a) -> Sorter a -> Sorter b
Sort by a derived value.
One way to use this is with collections of records:
Sort.list (Sort.by .name Sort.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.alphabetical
tiebreaker : Sorter b -> Sorter b -> Sorter b
Apply a tiebreaker Sorter
to use when the given Sorter
finds the
two values are equal.
This would be useful in a table that is sorted by multiple columns.
-- Without tiebreaker
Sort.list (Sort.by .name Sort.alphabetical)
[ { name = "Bo", cats = 4 }, { name = "Bo", cats = 2 }, { name = "Amy", cats = 3 } ]
--> [ { name = "Amy", cats = 3 }, { name = "Bo", cats = 4 }, { name = "Bo", cats = 2 } ]
-- With tiebreaker, the last two records are swapped
Sort.list (Sort.by .name Sort.alphabetical |> Sort.tiebreaker (Sort.by .cats Sort.increasing))
[ { name = "Bo", cats = 4 }, { name = "Bo", cats = 2 }, { name = "Amy", cats = 3 } ]
--> [ { name = "Amy", cats = 3 }, { name = "Bo", cats = 2 }, { name = "Bo", cats = 4 } ]
alphabetical : Sorter String
Sort 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" ]
increasing : Sorter number
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 ]
list : Sorter a -> List a -> List a
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