Skip to content
Subhajit Sahu edited this page Mar 13, 2020 · 29 revisions

Sorts based on compare function (optional).

array.sort(x, fn);
// x:  an array
// fn: compare function (a, b)
// --> sorted array
const array = require('extra-array');

var a = [-2, -3, 1, 4];
array.sort(a);

array.sort(a, (a, b) => Math.abs(a) - Math.abs(b));

var b = ['D', 'B', 'a', 'c'];
array.sort(b);

array.sort(b, (a, b) => a.toLowerCase().localeCompare(b.toLowerCase()));
// [ 'a', 'B', 'c', 'D' ]

references

Clone this wiki locally