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 _ from 'radash'
const users = [
{ id: 1, firstName: 'Alice', lastName: 'Smith' },
{ id: 3, firstName: 'Charlie', lastName: 'Brown' },
{ id: 2, firstName: 'Drew', lastName: 'Johnson' },
]
const compareById = _.castComparator('id')
users.sort(compareById)
// [Alice, Drew, Charlie]
const compareByFullName = _.castComparator(
user => `${user.firstName} ${user.lastName}`,
(a, b) => b.localeCompare(a),
)
users.sort(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.

const users = [
{ id: 1, firstName: 'Alice', lastName: 'Smith' },
{ id: 3, firstName: 'Charlie', lastName: 'Brown' },
{ id: 2, firstName: 'Drew', lastName: 'Johnson' },
]
const compareByFullName = _.castComparator(
user => `${user.firstName} ${user.lastName}`,
(a, b) => b.localeCompare(a),
)
users.sort(compareByFullName)
// [Alice, Charlie, Drew]