Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs.json

Large diffs are not rendered by default.

56 changes: 56 additions & 0 deletions src/Order/Extra.elm
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Order.Extra exposing
( explicit, byField, byFieldWith, byRank, ifStillTiedThen
, breakTies, breakTiesWith, reverse
, natural
, nothingLast, nothingFirst
, isOrdered, greaterThanBy, lessThanBy
)

Expand Down Expand Up @@ -90,6 +91,11 @@ to sort a deck of cards you can use `cardOrdering` directly:
@docs natural


# Maybes

@docs nothingLast, nothingFirst


# Utility

@docs isOrdered, greaterThanBy, lessThanBy
Expand Down Expand Up @@ -462,6 +468,56 @@ greaterThanBy ordering x y =



--- Maybe


{-| Returns an ordering that treats `Nothing` as greater than `Just a`.

[ Just 1, Nothing, Just 2 ]
|> List.sortWith (Order.Extra.nothingLast compare)
--> [ Just 1, Just 2, Nothing ]

-}
nothingLast : (a -> a -> Order) -> Maybe a -> Maybe a -> Order
nothingLast ordering x y =
case ( x, y ) of
( Nothing, Nothing ) ->
EQ

( Nothing, Just _ ) ->
GT

( Just _, Nothing ) ->
LT

( Just a, Just b ) ->
ordering a b


{-| Returns an ordering that treats `Nothing` as less than `Just a`.

[ Just 1, Nothing, Just 2 ]
|> List.sortWith (Order.Extra.nothingFirst compare)
--> [ Nothing, Just 1, Just 2 ]

-}
nothingFirst : (a -> a -> Order) -> Maybe a -> Maybe a -> Order
nothingFirst ordering x y =
case ( x, y ) of
( Nothing, Nothing ) ->
EQ

( Nothing, Just _ ) ->
LT

( Just _, Nothing ) ->
GT

( Just a, Just b ) ->
ordering a b



--- String


Expand Down