on master help sign in
Add sortWith
bbdc676Max Goldstein
libraries/List.elm+7−2
@@ -22,7 +22,7 @@ list must have the same type.
22@docs sum, product, maximum, minimum, all, any, and, or
22@docs sum, product, maximum, minimum, all, any, and, or
23
23
24# Sorting
24# Sorting
25−@docs sort, sortBy
25+@docs sort, sortBy, sortWith
26-}
26-}
27
27
28import open Basics
28import open Basics
@@ -232,6 +232,11 @@ repeat = Native.List.repeat
232sort : [comparable] -> [comparable]
232sort : [comparable] -> [comparable]
233sort = Native.List.sort
233sort = Native.List.sort
234
234
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). -}
236sortBy : (a -> comparable) -> [a] -> [a]
237sortBy : (a -> comparable) -> [a] -> [a]
237sortBy = Native.List.sortBy
238sortBy = 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) {
222 }));
222 }));
223 }
223 }
224
224
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+
225 function nth(xs, n) {
232 function nth(xs, n) {
226 return toArray(xs)[n];
233 return toArray(xs)[n];
227 }
234 }
@@ -301,6 +308,7 @@ Elm.Native.List.make = function(elm) {
301 zip:F2(zip),
308 zip:F2(zip),
302 sort:sort,
309 sort:sort,
303 sortBy:F2(sortBy),
310 sortBy:F2(sortBy),
311+ sortWith:F2(sortWith),
304 nth:F2(nth),
312 nth:F2(nth),
305 take:F2(take),
313 take:F2(take),
306 drop:F2(drop),
314 drop:F2(drop),