UNPKG

compare-by

Version:

A versatile utility library for sorting arrays of objects by one or multiple keys with customizable sort directions.

17 lines (15 loc) 435 B
import { CompareDirection } from './types'; /** * Compare numeric values. * @param {number} a The first value. * @param {number} b The second value. * @param {CompareDirection} dir The direction to compare. * @returns {number} 0 if values are equal. */ export const compareNumbers = (a: number, b: number, dir: CompareDirection = 'asc') => { if (dir === 'asc') { return a - b; } else { return b - a; } };