Skip to content

sort
131 bytes

Sort a list of objects by a numerical property

Usage

Given an array of objects, return a new array sorted by the numerical property specified in the get function. A third, and optional, argument allows you to sort in descending order instead of the default ascending order.

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 _
_
.
sort<{
name: string;
weight: number;
}>(array: readonly {
name: string;
weight: number;
}[], getter: (item: {
name: string;
weight: number;
}) => number, desc?: boolean): {
name: string;
weight: number;
}[]
export sort

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 _
_
.
sort<{
name: string;
weight: number;
}>(array: readonly {
name: string;
weight: number;
}[], getter: (item: {
name: string;
weight: number;
}) => number, desc?: boolean): {
name: string;
weight: number;
}[]
export sort

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]