Tools for composing comparison functions.
A comparison function is the type of function you can pass to List.sortWith.
You pass it two elements of a type and it returns an Order (defined in the Basics module and imported by deault).
Like Basics.min but using a custom comparator.
Compare.min (by String.length) "short" "longer"
--> "short"Like Basics.max but using a custom comparator.
Compare.max (by String.length) "short" "longer"
--> "longer"Like List.minimum but using a custom comparator.
minimum (by String.length) [ "longer", "short", "longest" ]
--> Just "short"Like List.maximum but using a custom comparator.
maximum (by String.length) [ "longer", "short", "longest" ]
--> Just "longest"Take a function meant for List.sortBy and turn it into a Comparator.
List.sortWith (by String.length) [ "longer", "short", "longest" ]
--> [ "short", "longer", "longest" ]Compare by multiple criteria in order. The first criterium that distinguishes two elements is used.
kara : { firstName : String, lastName : String }
kara = { firstName = "Kara", lastName = "Thrace" }
william : { firstName : String, lastName : String }
william = { firstName = "William", lastName = "Adama" }
lee : { firstName : String, lastName : String }
lee = { firstName = "Lee", lastName = "Adama" }
List.sortWith (concat [ by .lastName, by .firstName ]) [ kara, william, lee ]
--> [ lee, william, kara ]Reverse an ordering function.
List.sortWith (by String.length |> reverse) [ "longer", "short", "longest" ]
--> [ "longest", "longer", "short" ]
A function that returns the relative ordering of two elements of a type.