Returns a sorted version of data using a specified algorithm. Original array is left as-is

const data = [ 10, 2, 9, 5 ]
sort(data, `merge`); // [ 2, 5, 9, 20 ]

By default uses in-built semantics for comparison. But a function can be provided. Return 0 if a and b are equal, above 0 if a is considered higher than b or below zero if b is considered higher than a.

In the below example, the default sorting semantics are reversed:

const reverse = (a, b) => {
if (a === b) return 0;
if (a > b) return -1;
if (a < b) return 1;
return 0; // equal
}
sort(data, reverse); // [ 20, 9, 5, 2 ]
  • Type Parameters

    • T

    Parameters

    Returns T[]

    Sorted array