Options
All
• Public
• Public/Protected
• All

# Namespace Sort

## Sort Functions

• Sort an array in ascending order (least to greatest), while applying a function to each element to map the value before comparing.

Time complexity: O(1)

Space complexity: O(1)

example
``````const array = [{ value: 2 }, { value: 3 }, { value: 1 }, { value: 3 }];array.sort(Sort.ascending(x => x.value));
``````

#### Parameters

• ##### predicate: ((element: T) => Comparable)

A function that maps elements from type `T` to a `Comparable`

#### Returns CompareFn<T>

A compare function that returns a negative value if first argument is less than second argument, zero if they're equal and a positive value otherwise

• Sort an array in ascending order (least to greatest).

Time complexity: O(1)

Space complexity: O(1)

example
``````const array = [5, 3, 2, 4, 1];array.sort(Sort.ascending);
``````

#### Parameters

• ##### a: Comparable

Element to compare

• ##### b: Comparable

Element to compare

#### Returns number

A negative value if first argument is less than second argument, zero if they're equal and a positive value otherwise

• Sort an array in descending order (greatest to least), while applying a function to each element to map the value before comparing.

Time complexity: O(1)

Space complexity: O(1)

example
``````const array = [{ value: 2 }, { value: 3 }, { value: 1 }, { value: 3 }];array.sort(Sort.descending(x => x.value));
``````

#### Parameters

• ##### predicate: ((element: T) => Comparable)

A function that maps elements from type `T` to a `Comparable`

#### Returns CompareFn<T>

A compare function that returns a positive value if first argument is less than second argument, zero if they're equal and a negative value otherwise

• Sort an array in descending order (greatest to least).

Time complexity: O(1)

Space complexity: O(1)

example
``````const array = [5, 3, 2, 4, 1];array.sort(Sort.descending);
``````

#### Parameters

• ##### a: Comparable

Element to compare

• ##### b: Comparable

Element to compare

#### Returns number

A positive value if first argument is less than second argument, zero if they're equal and a negative value otherwise

Generated using TypeDoc