3
Performs a multi-level three-way comparison on numbers/strings and returns 1, 0, or -1.
Primarily useful in complex sort()
callbacks.
Installation
npm install 3 --save
Usage
const compare3Way = require('3')
const people = [
{name: 'John', age: 27},
{name: 'Stephen', age: 26},
{name: 'John', age: 25},
]
people.sort((a, b) => compare3Way(a.name, b.name))
people.sort((a, b) => compare3Way(a.age, b.age))
people.sort((a, b) => compare3Way(a.name, b.name, a.age, b.age))
compare3Way('', 'str')
compare3Way('', 'str', {emptyStringsLast: true})