Skip to content

sort

Sort a list of items by a numerical property or value

137 bytes
since v12.1.0

Usage

Given an array, return a new array sorted either by the numerical property specified in the get function or the numerical value of its items if no get function is passed. A third, and optional, argument allows you to sort in descending order instead of the default ascending order.

The function preserves the original order of items with equal values.

This function only supports numerical sorting. For alphabetic sorting, see the alphabetical function.

import * as
import _
_
from 'radashi'
const
const fish: {
name: string;
weight: number;
}[]
fish
= [
{
name: string
name
: 'Marlin',
weight: number
weight
: 105,
},
{
name: string
name
: 'Bass',
weight: number
weight
: 8,
},
{
name: string
name
: 'Trout',
weight: number
weight
: 13,
},
]
import _
_
.
function sort<{
name: string;
weight: number;
}[]>(array: {
name: string;
weight: number;
}[], getter?: (item: {
name: string;
weight: number;
}) => number, desc?: boolean): {
name: string;
weight: number;
}[]

Sort an array without modifying it and return the newly sorted value.

@seehttps://radashi.js.org/reference/array/sort

@example

const fish = [
{ name: 'Marlin', weight: 105 },
{ name: 'Bass', weight: 8 },
{ name: 'Trout', weight: 13 }
]
sort(fish, f => f.weight) // => [Bass, Trout, Marlin]
sort(fish, f => f.weight, true) // => [Marlin, Trout, Bass]

@version12.1.0

sort
(
const fish: {
name: string;
weight: number;
}[]
fish
,
f: {
name: string;
weight: number;
}
f
=>
f: {
name: string;
weight: number;
}
f
.
weight: number
weight
) // => [bass, trout, marlin]
import _
_
.
function sort<{
name: string;
weight: number;
}[]>(array: {
name: string;
weight: number;
}[], getter?: (item: {
name: string;
weight: number;
}) => number, desc?: boolean): {
name: string;
weight: number;
}[]

Sort an array without modifying it and return the newly sorted value.

@seehttps://radashi.js.org/reference/array/sort

@example

const fish = [
{ name: 'Marlin', weight: 105 },
{ name: 'Bass', weight: 8 },
{ name: 'Trout', weight: 13 }
]
sort(fish, f => f.weight) // => [Bass, Trout, Marlin]
sort(fish, f => f.weight, true) // => [Marlin, Trout, Bass]

@version12.1.0

sort
(
const fish: {
name: string;
weight: number;
}[]
fish
,
f: {
name: string;
weight: number;
}
f
=>
f: {
name: string;
weight: number;
}
f
.
weight: number
weight
, true) // => [marlin, trout, bass]
const
const numbers: number[]
numbers
= [2, 0, 1]
import _
_
.
function sort<number[]>(array: number[], getter?: (item: number) => number, desc?: boolean): number[]

Sort an array without modifying it and return the newly sorted value.

@seehttps://radashi.js.org/reference/array/sort

@example

const fish = [
{ name: 'Marlin', weight: 105 },
{ name: 'Bass', weight: 8 },
{ name: 'Trout', weight: 13 }
]
sort(fish, f => f.weight) // => [Bass, Trout, Marlin]
sort(fish, f => f.weight, true) // => [Marlin, Trout, Bass]

@version12.1.0

sort
(
const numbers: number[]
numbers
) // => [0, 1, 2]
import _
_
.
function sort<number[]>(array: number[], getter?: (item: number) => number, desc?: boolean): number[]

Sort an array without modifying it and return the newly sorted value.

@seehttps://radashi.js.org/reference/array/sort

@example

const fish = [
{ name: 'Marlin', weight: 105 },
{ name: 'Bass', weight: 8 },
{ name: 'Trout', weight: 13 }
]
sort(fish, f => f.weight) // => [Bass, Trout, Marlin]
sort(fish, f => f.weight, true) // => [Marlin, Trout, Bass]

@version12.1.0

sort
(
const numbers: number[]
numbers
,
import _
_
.
function identity(): undefined (+1 overload)

A function that returns the value passed to it.

@seehttps://radashi.js.org/reference/function/identity

@example

identity() // => undefined
identity(1) // => 1
identity("a") // => "a"

@version12.7.0

identity
, true) // => [2, 1, 0]