Skip to content

Commit 82f57f4

Browse files
committed
Add lists.sort
1 parent 2a4808f commit 82f57f4

File tree

1 file changed

+21
-0
lines changed
  • src/javascript/lib/core/erlang_compat

1 file changed

+21
-0
lines changed

src/javascript/lib/core/erlang_compat/lists.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,26 @@ function splitwith(pred, list) {
220220
return new ErlangTypes.Tuple(list1, list2);
221221
}
222222

223+
function sort(...args) {
224+
if (args.length === 1) {
225+
const list2 = [...args[0]];
226+
return list2.sort();
227+
}
228+
229+
const fun = args[0];
230+
const list2 = [...args[1]];
231+
232+
return list2.sort((a, b) => {
233+
const result = fun(a, b);
234+
235+
if (result === true) {
236+
return -1;
237+
}
238+
239+
return 1;
240+
});
241+
}
242+
223243
export default {
224244
reverse,
225245
foreach,
@@ -243,4 +263,5 @@ export default {
243263
all,
244264
any,
245265
splitwith,
266+
sort,
246267
};

0 commit comments

Comments
 (0)