Add sortWith

This commit is contained in:
Max Goldstein 2013-11-21 16:33:30 -05:00
parent b3c598466b
commit bbdc6760c1
2 changed files with 15 additions and 2 deletions

View file

@ -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

View file

@ -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),