rtfeldman/elm-sorter-experiment/2.1.0

NOTE: the latest version is 2.1.2

Rerouted (retagged, verified). The tag now points at a different commit than when this version was published. The Lamdera package server serves a copy recovered from https://github.com/rtfeldman/elm-sorter-experiment.

What we checked: Tag 2.1.0 now points at 76d3ee545ee4, not the published commit. Rebuilt with git archive from commit 70c636c297ef of https://github.com/rtfeldman/elm-sorter-experiment (zip prefix rtfeldman-elm-sorter-experiment-70c636c/, America/Los_Angeles times): the zip is byte-identical to the one package.elm-lang.org recorded, SHA-1 a6ae57db2a43d6f13840e5b912bf0b70134eeb7b.

The Lamdera compiler uses this copy and prints a one-line notice saying so. elm itself fails to download this version.

Details · package.elm-lang.org's zip: https://github.com/rtfeldman/elm-sorter-experiment/zipball/2.1.0/

Sort

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.

Types

type Sorter a

A description of how to sort values of a particular type.

Conversions

custom : (a -> a -> Order) -> Sorter a

Create a custom Sorter by defining how to order two values.

toOrder : Sorter a -> a -> a -> Order

Determine an Order for two values, based on the given Sorter

Composing Sorters

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

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 } ]

Primitives

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" ]

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 ]

Collections

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