diff --git a/src/Core__Array.mjs b/src/Core__Array.mjs index e7127f01..f675dff5 100644 --- a/src/Core__Array.mjs +++ b/src/Core__Array.mjs @@ -21,6 +21,12 @@ function lastIndexOfOpt(arr, item) { } +function sort(arr, cmp) { + var result = arr.slice(); + result.sort(cmp); + return result; +} + function reduce(a, x, f) { var f$1 = Curry.__2(f); var r = x; @@ -107,6 +113,7 @@ function flatMap(a, f) { } export { + sort , indexOfOpt , lastIndexOfOpt , reduce , diff --git a/src/Core__Array.res b/src/Core__Array.res index 43d80d2d..658f45c8 100644 --- a/src/Core__Array.res +++ b/src/Core__Array.res @@ -39,8 +39,6 @@ external copyWithin: (array<'a>, ~target: int, ~start: int, ~end: int) => array< @send external shift: array<'a> => option<'a> = "shift" -@send external sortInPlace: (array<'a>, ('a, 'a) => int) => unit = "sort" - @variadic @send external spliceInPlace: (array<'a>, ~start: int, ~remove: int, ~insert: array<'a>) => unit = "splice" @@ -78,6 +76,14 @@ let lastIndexOfOpt = (arr, item) => @send external sliceToEnd: (array<'a>, ~start: int) => array<'a> = "slice" @send external copy: array<'a> => array<'a> = "slice" +@send external sortInPlace: (array<'a>, ('a, 'a) => int) => unit = "sort" + +let sort = (arr, cmp) => { + let result = copy(arr) + sortInPlace(result, cmp) + result +} + @send external toString: array<'a> => string = "toString" @send external toLocaleString: array<'a> => string = "toLocaleString" diff --git a/src/Core__Array.resi b/src/Core__Array.resi index e97aeaa0..562f739d 100644 --- a/src/Core__Array.resi +++ b/src/Core__Array.resi @@ -20,6 +20,7 @@ external copyWithin: (array<'a>, ~target: int, ~start: int, ~end: int) => array< @variadic @send external pushMany: (array<'a>, array<'a>) => unit = "push" @send external reverseInPlace: array<'a> => unit = "reverse" @send external shift: array<'a> => option<'a> = "shift" +let sort: (array<'a>, ('a, 'a) => int) => array<'a> @send external sortInPlace: (array<'a>, ('a, 'a) => int) => unit = "sort" @variadic @send external spliceInPlace: (array<'a>, ~start: int, ~remove: int, ~insert: array<'a>) => unit = diff --git a/src/Core__List.mjs b/src/Core__List.mjs index 6d3a026e..8da20a23 100644 --- a/src/Core__List.mjs +++ b/src/Core__List.mjs @@ -4,7 +4,6 @@ import * as Curry from "rescript/lib/es6/curry.js"; import * as Belt_Array from "rescript/lib/es6/belt_Array.js"; import * as Caml_option from "rescript/lib/es6/caml_option.js"; import * as Core__Array from "./Core__Array.mjs"; -import * as Belt_SortArray from "rescript/lib/es6/belt_SortArray.js"; function head(x) { if (x) { @@ -1177,9 +1176,8 @@ function setAssoc(xs, x, k, eq) { } function sort(xs, cmp) { - var cmp$1 = Curry.__2(cmp); var arr = toArray(xs); - Belt_SortArray.stableSortInPlaceByU(arr, cmp$1); + arr.sort(cmp); return fromArray(arr); } diff --git a/src/Core__List.res b/src/Core__List.res index 88924bb7..27c248e7 100644 --- a/src/Core__List.res +++ b/src/Core__List.res @@ -793,14 +793,12 @@ let setAssocU = (xs, x, k, eq) => let setAssoc = (xs, x, k, eq) => setAssocU(xs, x, k, (. a, b) => eq(a, b)) -let sortU = (xs, cmp) => { +let sort = (xs, cmp) => { let arr = toArray(xs) - Belt_SortArray.stableSortInPlaceByU(arr, cmp) + Core__Array.sortInPlace(arr, cmp) fromArray(arr) } -let sort = (xs, cmp) => sortU(xs, (. x, y) => cmp(x, y)) - let rec getByU = (xs, p) => switch xs { | list{} => None