Add sortWith
This commit is contained in:
parent
b3c598466b
commit
bbdc6760c1
2 changed files with 15 additions and 2 deletions
|
@ -22,7 +22,7 @@ list must have the same type.
|
|||
@docs sum, product, maximum, minimum, all, any, and, or
|
||||
|
||||
# Sorting
|
||||
@docs sort, sortBy
|
||||
@docs sort, sortBy, sortWith
|
||||
-}
|
||||
|
||||
import open Basics
|
||||
|
@ -232,6 +232,11 @@ repeat = Native.List.repeat
|
|||
sort : [comparable] -> [comparable]
|
||||
sort = Native.List.sort
|
||||
|
||||
{-| Sort a list by a given comparison function. -}
|
||||
{-| Sort a list by a given comparison transformation, such a [record field
|
||||
accessors](http://elm-lang.org/learn/Syntax.elm#records). -}
|
||||
sortBy : (a -> comparable) -> [a] -> [a]
|
||||
sortBy = Native.List.sortBy
|
||||
|
||||
{-| Sort a list by a given comparison function. -}
|
||||
sortWith : (a -> a -> Order) -> [a] -> [a]
|
||||
sortWith = Native.List.sortWith
|
||||
|
|
|
@ -222,6 +222,13 @@ Elm.Native.List.make = function(elm) {
|
|||
}));
|
||||
}
|
||||
|
||||
function sortWith(f, xs) {
|
||||
return fromArray(toArray(xs).sort(function(a,b){
|
||||
var ord = f(a)(b).ctor;
|
||||
return ord === 'EQ' ? 0 : ord === 'LT' ? -1 : 1;
|
||||
}));
|
||||
}
|
||||
|
||||
function nth(xs, n) {
|
||||
return toArray(xs)[n];
|
||||
}
|
||||
|
@ -301,6 +308,7 @@ Elm.Native.List.make = function(elm) {
|
|||
zip:F2(zip),
|
||||
sort:sort,
|
||||
sortBy:F2(sortBy),
|
||||
sortWith:F2(sortWith),
|
||||
nth:F2(nth),
|
||||
take:F2(take),
|
||||
drop:F2(drop),
|
||||
|
|
Loading…
Reference in a new issue