You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

thenby

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

thenby

Micro library for sorting arrays using the firstBy().thenBy().thenBy() syntax

1.3.4
latest
Source
npmnpm
Version published
Weekly downloads
859K
5.7%
Maintainers
1
Weekly downloads
 
Created

What is thenby?

The 'thenby' npm package is a utility for performing multi-level sorting of arrays. It allows you to sort arrays by multiple criteria in a concise and readable manner.

What are thenby's main functionalities?

Basic Multi-Level Sorting

This feature allows you to sort an array of objects first by the 'name' property and then by the 'age' property. The 'thenBy' method is chained to add additional sorting criteria.

const firstBy = require('thenby');

const data = [
  { name: 'John', age: 30, city: 'New York' },
  { name: 'Jane', age: 25, city: 'San Francisco' },
  { name: 'John', age: 25, city: 'Los Angeles' },
  { name: 'Jane', age: 30, city: 'Chicago' }
];

const sortedData = data.sort(
  firstBy('name')
  .thenBy('age')
);

console.log(sortedData);

Custom Comparator Functions

This feature allows you to use custom comparator functions for sorting. In this example, the 'name' property is sorted using 'localeCompare' for string comparison, and the 'age' property is sorted using a numerical comparison.

const firstBy = require('thenby');

const data = [
  { name: 'John', age: 30, city: 'New York' },
  { name: 'Jane', age: 25, city: 'San Francisco' },
  { name: 'John', age: 25, city: 'Los Angeles' },
  { name: 'Jane', age: 30, city: 'Chicago' }
];

const sortedData = data.sort(
  firstBy((a, b) => a.name.localeCompare(b.name))
  .thenBy((a, b) => a.age - b.age)
);

console.log(sortedData);

Descending Order Sorting

This feature allows you to sort in descending order by specifying the 'direction' option. In this example, both 'name' and 'age' properties are sorted in descending order.

const firstBy = require('thenby');

const data = [
  { name: 'John', age: 30, city: 'New York' },
  { name: 'Jane', age: 25, city: 'San Francisco' },
  { name: 'John', age: 25, city: 'Los Angeles' },
  { name: 'Jane', age: 30, city: 'Chicago' }
];

const sortedData = data.sort(
  firstBy('name', { direction: -1 })
  .thenBy('age', { direction: -1 })
);

console.log(sortedData);

Other packages similar to thenby

Keywords

sort

FAQs

Package last updated on 04 Jul 2020

Did you know?

Socket

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