Skip to content
wolfram77 edited this page Apr 13, 2020 · 29 revisions

Arranges values in an order. 🏃 📼 📦 🌔

Alternatives: compare, compare-update, map, map-update.

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

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

array.sort(x, (a, b) => Math.abs(a) - Math.abs(b));
// [ 1, -2, -3, 4 ]

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

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

references

Clone this wiki locally