on master help sign in
Add sortWith
commitbbdc6760c17913d6a1f51f749ad741cbdbd6b71c authorMax Goldstein <maxgoldstein1@gmail.com> dateThu 21 Nov 2013, 16:33 (13 years ago) parentb3c5984
2 files changed+15−2
libraries/List.elm+7−2 libraries/Native/List.js+8
libraries/List.elm+7−2
@@ -22,7 +22,7 @@ list must have the same type.
2222@docs sum, product, maximum, minimum, all, any, and, or
2323
2424# Sorting
25−@docs sort, sortBy
25+@docs sort, sortBy, sortWith
2626-}
2727
2828import open Basics
@@ -232,6 +232,11 @@ repeat = Native.List.repeat
232232sort : [comparable] -> [comparable]
233233sort = Native.List.sort
234234
235−{-| Sort a list by a given comparison function. -}
235+{-| Sort a list by a given comparison transformation, such a [record field
236+accessors](http://elm-lang.org/learn/Syntax.elm#records). -}
236237sortBy : (a -> comparable) -> [a] -> [a]
237238sortBy = Native.List.sortBy
239+
240+{-| Sort a list by a given comparison function. -}
241+sortWith : (a -> a -> Order) -> [a] -> [a]
242+sortWith = Native.List.sortWith
libraries/Native/List.js+8
@@ -222,6 +222,13 @@ Elm.Native.List.make = function(elm) {
222222 }));
223223 }
224224
225+ function sortWith(f, xs) {
226+ return fromArray(toArray(xs).sort(function(a,b){
227+ var ord = f(a)(b).ctor;
228+ return ord === 'EQ' ? 0 : ord === 'LT' ? -1 : 1;
229+ }));
230+ }
231+
225232 function nth(xs, n) {
226233 return toArray(xs)[n];
227234 }
@@ -301,6 +308,7 @@ Elm.Native.List.make = function(elm) {
301308 zip:F2(zip),
302309 sort:sort,
303310 sortBy:F2(sortBy),
311+ sortWith:F2(sortWith),
304312 nth:F2(nth),
305313 take:F2(take),
306314 drop:F2(drop),