Skip to content

castComparator

Cast a value into a comparator function

230 bytes

Usage

Create a comparator function which can be passed into Array.prototype.sort. It accepts either a property name or a mapping function. Optionally, you can pass a custom compare function (e.g. for localeCompare use cases).

The first argument of castComparator is called the mapping. This can be either:

  • Function: If mapping is a function, it maps the input values to a comparable value.
  • Property Name: If mapping is a property name, it maps the input values to a property of the input values with a comparable value.
import * as
import _
_
from 'radashi'
const
const users: {
id: number;
firstName: string;
lastName: string;
}[]
users
= [
{
id: number
id
: 1,
firstName: string
firstName
: 'Alice',
lastName: string
lastName
: 'Smith' },
{
id: number
id
: 3,
firstName: string
firstName
: 'Charlie',
lastName: string
lastName
: 'Brown' },
{
id: number
id
: 2,
firstName: string
firstName
: 'Drew',
lastName: string
lastName
: 'Johnson' },
]
const
const compareById: _.Comparator<{
id: _.Comparable;
}>
compareById
=
import _
_
.
castComparator<"id">(mapping: "id", compare?: null | undefined, reverse?: boolean): _.Comparator<{
id: _.Comparable;
}> (+4 overloads)
export castComparator

Cast a value into a comparator function.

  • Function: If mapping is a function, it maps the input values to a comparable value.
  • Property Name: If mapping is a property name, it maps the input values to a property of the input values with a comparable value.

Optionally, you can pass a custom compare function that receives the mapped values and returns a number. If not provided, values are compared with the < and > built-in operators. A positive number means the “right value” is greater than the “left value”, a negative number means the “left value” is greater than the “right value”, and 0 means both values are equal.

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

@example

const compareUserNames = castComparator(
(user) => user.name,
(a, b) => b.localeCompare(a),
)
const users = [
{ name: 'John', age: 20 },
{ name: 'Jane', age: 25 },
{ name: 'Doe', age: 22 },
]
users.sort(compareUserNames)
// => [Doe, Jane, John]

@version12.2.0

castComparator
('id')
const users: {
id: number;
firstName: string;
lastName: string;
}[]
users
.
Array<{ id: number; firstName: string; lastName: string; }>.sort(compareFn?: ((a: {
id: number;
firstName: string;
lastName: string;
}, b: {
id: number;
firstName: string;
lastName: string;
}) => number) | undefined): {
id: number;
firstName: string;
lastName: string;
}[]

Sorts an array in place. This method mutates the array and returns a reference to the same array.

@param

compareFn Function used to determine the order of the elements. It is expected to return a negative value if the first argument is less than the second argument, zero if they're equal, and a positive value otherwise. If omitted, the elements are sorted in ascending, ASCII character order.

[11,2,22,1].sort((a, b) => a - b)

sort
(
const compareById: _.Comparator<{
id: _.Comparable;
}>
compareById
)
// [Alice, Drew, Charlie]
const
const compareByFullName: _.Comparator<{
id: number;
firstName: string;
lastName: string;
}>
compareByFullName
=
import _
_
.
castComparator<unknown, (user: (typeof users)[number]) => string>(mapping: (user: (typeof users)[number]) => string, compare: _.Comparator<unknown>, reverse?: boolean): _.Comparator<{
id: number;
firstName: string;
lastName: string;
}> (+4 overloads)
export castComparator

Cast a value into a comparator function.

  • Function: If mapping is a function, it maps the input values to a comparable value.
  • Property Name: If mapping is a property name, it maps the input values to a property of the input values with a comparable value.

Optionally, you can pass a custom compare function that receives the mapped values and returns a number. If not provided, values are compared with the < and > built-in operators. A positive number means the “right value” is greater than the “left value”, a negative number means the “left value” is greater than the “right value”, and 0 means both values are equal.

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

@example

const compareUserNames = castComparator(
(user) => user.name,
(a, b) => b.localeCompare(a),
)
const users = [
{ name: 'John', age: 20 },
{ name: 'Jane', age: 25 },
{ name: 'Doe', age: 22 },
]
users.sort(compareUserNames)
// => [Doe, Jane, John]

@version12.2.0

castComparator
(
(
user: {
id: number;
firstName: string;
lastName: string;
}
user
: typeof
const users: {
id: number;
firstName: string;
lastName: string;
}[]
users
[number]) => `${
user: {
id: number;
firstName: string;
lastName: string;
}
user
.
firstName: string
firstName
} ${
user: {
id: number;
firstName: string;
lastName: string;
}
user
.
lastName: string
lastName
}`,
(
a: unknown
a
,
b: unknown
b
) =>
b: unknown
b
.
any
localeCompare
(
a: unknown
a
),
)
const users: {
id: number;
firstName: string;
lastName: string;
}[]
users
.
Array<{ id: number; firstName: string; lastName: string; }>.sort(compareFn?: ((a: {
id: number;
firstName: string;
lastName: string;
}, b: {
id: number;
firstName: string;
lastName: string;
}) => number) | undefined): {
id: number;
firstName: string;
lastName: string;
}[]

Sorts an array in place. This method mutates the array and returns a reference to the same array.

@param

compareFn Function used to determine the order of the elements. It is expected to return a negative value if the first argument is less than the second argument, zero if they're equal, and a positive value otherwise. If omitted, the elements are sorted in ascending, ASCII character order.

[11,2,22,1].sort((a, b) => a - b)

sort
(
const compareByFullName: _.Comparator<{
id: number;
firstName: string;
lastName: string;
}>
compareByFullName
)
// [Alice, Charlie, Drew]

Compare Function

Optionally, you can pass a custom compare function that receives the mapped values and returns a number. If not provided, values are compared with the < and > built-in operators.

A positive number means the “right value” is greater than the “left value”, a negative number means the “left value” is greater than the “right value”, and 0 means both values are equal.

import * as
import _
_
from 'radashi'
const
const users: {
id: number;
firstName: string;
lastName: string;
}[]
users
= [
{
id: number
id
: 1,
firstName: string
firstName
: 'Alice',
lastName: string
lastName
: 'Smith' },
{
id: number
id
: 3,
firstName: string
firstName
: 'Charlie',
lastName: string
lastName
: 'Brown' },
{
id: number
id
: 2,
firstName: string
firstName
: 'Drew',
lastName: string
lastName
: 'Johnson' },
]
const
const compareByFullName: _.Comparator<{
id: number;
firstName: string;
lastName: string;
}>
compareByFullName
=
import _
_
.
castComparator<unknown, (user: (typeof users)[number]) => string>(mapping: (user: (typeof users)[number]) => string, compare: _.Comparator<unknown>, reverse?: boolean): _.Comparator<{
id: number;
firstName: string;
lastName: string;
}> (+4 overloads)
export castComparator

Cast a value into a comparator function.

  • Function: If mapping is a function, it maps the input values to a comparable value.
  • Property Name: If mapping is a property name, it maps the input values to a property of the input values with a comparable value.

Optionally, you can pass a custom compare function that receives the mapped values and returns a number. If not provided, values are compared with the < and > built-in operators. A positive number means the “right value” is greater than the “left value”, a negative number means the “left value” is greater than the “right value”, and 0 means both values are equal.

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

@example

const compareUserNames = castComparator(
(user) => user.name,
(a, b) => b.localeCompare(a),
)
const users = [
{ name: 'John', age: 20 },
{ name: 'Jane', age: 25 },
{ name: 'Doe', age: 22 },
]
users.sort(compareUserNames)
// => [Doe, Jane, John]

@version12.2.0

castComparator
(
(
user: {
id: number;
firstName: string;
lastName: string;
}
user
: typeof
const users: {
id: number;
firstName: string;
lastName: string;
}[]
users
[number]) => `${
user: {
id: number;
firstName: string;
lastName: string;
}
user
.
firstName: string
firstName
} ${
user: {
id: number;
firstName: string;
lastName: string;
}
user
.
lastName: string
lastName
}`,
(
a: unknown
a
,
b: unknown
b
) =>
b: unknown
b
.
any
localeCompare
(
a: unknown
a
),
)
const users: {
id: number;
firstName: string;
lastName: string;
}[]
users
.
Array<{ id: number; firstName: string; lastName: string; }>.sort(compareFn?: ((a: {
id: number;
firstName: string;
lastName: string;
}, b: {
id: number;
firstName: string;
lastName: string;
}) => number) | undefined): {
id: number;
firstName: string;
lastName: string;
}[]

Sorts an array in place. This method mutates the array and returns a reference to the same array.

@param

compareFn Function used to determine the order of the elements. It is expected to return a negative value if the first argument is less than the second argument, zero if they're equal, and a positive value otherwise. If omitted, the elements are sorted in ascending, ASCII character order.

[11,2,22,1].sort((a, b) => a - b)

sort
(
const compareByFullName: _.Comparator<{
id: number;
firstName: string;
lastName: string;
}>
compareByFullName
)
// [Alice, Charlie, Drew]