arraySort

arraySort

> Functions

Sorts the list array according to the order defined by the callback function.

The elements are compared in pairs: the callback function must return a negative value, 0 or a positive value depending on whether the first value comes before, at the same level as, or after the second value.

The default order is:

In LS1, the default order is: numbers, booleans, arrays, null elements, then strings.

Example:

Parameters
Return
TypeScript

In JavaScript, sort() without a comparator sorts the elements as strings: [10, 9, 1] gives [1, 10, 9], not the numeric order of arraySort. Always pass a comparator for numbers: [...a].sort((x, y) => x - y), and (x, y) => y - x for descending order. sort sorts in place, hence the [...a] copy to get a new array as arraySort does; a.toSorted((x, y) => x - y) does both at once.

Python

In Python, sorted(a) returns a new sorted list, like arraySort. But there is no two-argument comparator: you sort by a key, sorted(a, key=len) or sorted(a, key=lambda e: e.life), and reverse=True reverses the order. To reuse a comparator that returns a negative, zero or positive number, wrap it: sorted(a, key=functools.cmp_to_key(comparator)). Sorting compares the elements with each other: a list mixing numbers and strings raises a TypeError.

Notes