🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more

sort-on

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sort-on

Sort an array on an object property

6.1.0
latest
Version published
Weekly downloads
330K
-14.16%
Maintainers
1
Weekly downloads
 
Created

What is sort-on?

The sort-on npm package allows you to sort an array of objects based on the values of specific properties. It provides a simple and efficient way to perform sorting operations on complex data structures.

What are sort-on's main functionalities?

Sort by a single property

This feature allows you to sort an array of objects by a single property. In this example, the array of objects is sorted by the 'age' property.

const sortOn = require('sort-on');
const data = [
  {name: 'John', age: 25},
  {name: 'Jane', age: 22},
  {name: 'Jim', age: 30}
];
const sortedData = sortOn(data, 'age');
console.log(sortedData);

Sort by multiple properties

This feature allows you to sort an array of objects by multiple properties. In this example, the array is first sorted by 'age' and then by 'height' for objects with the same age.

const sortOn = require('sort-on');
const data = [
  {name: 'John', age: 25, height: 180},
  {name: 'Jane', age: 22, height: 170},
  {name: 'Jim', age: 22, height: 175}
];
const sortedData = sortOn(data, ['age', 'height']);
console.log(sortedData);

Sort by nested properties

This feature allows you to sort an array of objects by nested properties. In this example, the array is sorted by the 'age' property within the 'details' object.

const sortOn = require('sort-on');
const data = [
  {name: 'John', details: {age: 25}},
  {name: 'Jane', details: {age: 22}},
  {name: 'Jim', details: {age: 30}}
];
const sortedData = sortOn(data, 'details.age');
console.log(sortedData);

Other packages similar to sort-on

FAQs

Package last updated on 15 Aug 2024

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