Skip to content

sort ​

The sort() method sorts the elements of an array using a custom comparison function and returns a new sorted array.

Interactive example ​

Syntax ​

Classic signature ​

typescript
function sort<
	GenericElement extends unknown
>(
	input: readonly GenericElement[],
	compareFn: (
		first: GenericElement, 
		second: GenericElement
	) => number
): GenericElement[]

Curried signature ​

typescript
function sort<
	GenericElement extends unknown
>(
	compareFn: (
		first: GenericElement, 
		second: GenericElement
	) => number
): (input: readonly GenericElement[]) => GenericElement[]

Parameters ​

  • input: The array to sort.
  • compareFn: Function that compares two elements. It must return a negative number, zero, or a positive number to define the relative order.

Return value ​

A new array containing the same elements sorted according to the provided function. The initial array is never modified.

See also ​

  • reverse - Reverses the order of the elements

Sources ​

Released under the MIT license.