Socket
Socket
Sign inDemoInstall

3

Package Overview
Dependencies
3
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    3

Performs a multi-level three-way comparison on numbers/strings and returns 1, 0, or -1.


Version published
Weekly downloads
1.8K
increased by1.55%
Maintainers
1
Install size
43.6 kB
Created
Weekly downloads
 

Readme

Source

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},
]

// Sort by name
people.sort((a, b) => compare3Way(a.name, b.name)) // John, John, Stephen

// Sort by age
people.sort((a, b) => compare3Way(a.age, b.age)) // John, Stephen, John

// Multi-level sort: First by name, then by age
people.sort((a, b) => compare3Way(a.name, b.name, a.age, b.age)) // John (#2), John (#1), Stephen

// Normally empty strings are sorted so they come first,
// but you can use the `emptyStringsLast` option to change that.
compare3Way('', 'str') // -1
compare3Way('', 'str', {emptyStringsLast: true}) // 1

Keywords

FAQs

Last updated on 03 Feb 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc