Skip to content

Function: sort()

Call Signature

sort<K, V, T>(data, propertyName, options?): T

Defined in: packages/core/src/iterable/sort.ts:48

Sort iterable lists (Array/Map/Set).

Type Parameters

K

K

V

V extends Record<PropertyKey, unknown>

T

T extends IterableList<K, V>

Parameters

data

T

propertyName

keyof V & string

Accepted values:

  • string: value object property name
  • function: comparator function. Recommended for performance.
  • true: indicates to sort by Map keys instead of values.

options?

SortOptions

(optional) extra sorting opitons

Returns

T

sorted map

Example

Sort map of objects

javascript
import { sort } from '@superutils/core'

const map = new Map([
    [0, { name: 'Charlie' }],
    [1, { name: 'Alice' }],
    [2, { name: 'Bob' }],
])
console.log(sort(map, 'name'))
// result: Map(3) {
//   1 => { name: 'Alice' },
//   2 => { name: 'Bob' },
//   0 => { name: 'Charlie' }
// }

Call Signature

sort<K, V>(data, byKey?, options?): Map<K, V>

Defined in: packages/core/src/iterable/sort.ts:70

Sort Map by map-keys K

Type Parameters

K

K

V

V

Parameters

data

Map<K, V>

byKey?

boolean

Whether to sort by key or Value

options?

SortOptions

Returns

Map<K, V>

Example

Sort map of simple values (string/number/boolean)

javascript
import { sort } from '@superutils/core'

const map = new Map([
	   [1, 1],
	   [2, 2],
    [0, 0],
])
console.log(sort(map, false, { asString: false }))
// result: Map(3) { 0 => 0, 1 => 1, 2 => 2 }

Call Signature

sort<K, V>(map, comparator, options?): Map<K, V>

Defined in: packages/core/src/iterable/sort.ts:77

Sort Map with comparator function

Type Parameters

K

K

V

V

Parameters

map

Map<K, V>

comparator

EntryComparator<K, V>

options?

SortOptions

Returns

Map<K, V>

Call Signature

sort<V>(array, comparator, options?): V[]

Defined in: packages/core/src/iterable/sort.ts:83

Sort Array with comparator function

Type Parameters

V

V

Parameters

array

V[]

comparator

ArrayComparator<V>

options?

SortOptions

Returns

V[]

Call Signature

sort<V>(set, comparator, options?): Set<V>

Defined in: packages/core/src/iterable/sort.ts:89

Sort Set with comparator function

Type Parameters

V

V

Parameters

set

Set<V>

comparator

ArrayComparator<V>

options?

SortOptions

Returns

Set<V>

Call Signature

sort<K, V, T>(data, options?): T

Defined in: packages/core/src/iterable/sort.ts:95

Sort Array/Map/Set with string | boolean | number values

Type Parameters

K

K

V

V extends string | number | boolean

T

T extends IterableList<K, V>

Parameters

data

T

options?

SortOptions

Returns

T