UNPKG

compare-by

Version:

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

18 lines (16 loc) 507 B
import { CompareDirection } from './types'; /** * Compare string values. * @description uses `String.localCompare()`. * @param {string} a The first value. * @param {string} b The second value. * @param {CompareDirection} dir The direction to compare. * @returns {number} 0 if values are equal. */ export const compareStrings = (a: string, b: string, dir: CompareDirection = 'asc') => { if (dir === 'asc') { return a.localeCompare(b); } else { return b.localeCompare(a); } };