Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

sort-array

Package Overview
Dependencies
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sort-array

Isomorphic, load-anywhere function to sort an array by scalar, deep or computed values in any standard or custom order

  • 5.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
306K
increased by9.73%
Maintainers
1
Weekly downloads
 
Created

What is sort-array?

The sort-array npm package is a utility for sorting arrays of objects or primitive values. It provides a simple and flexible API to sort arrays based on various criteria, including multiple fields and custom comparator functions.

What are sort-array's main functionalities?

Sort by Single Field

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

const sortArray = require('sort-array');
const data = [{ name: 'John', age: 30 }, { name: 'Jane', age: 25 }, { name: 'Jim', age: 35 }];
const sortedData = sortArray(data, { by: 'age' });
console.log(sortedData);

Sort by Multiple Fields

This feature allows you to sort an array of objects by multiple fields. In this example, the array is first sorted by 'age' and then by 'name'.

const sortArray = require('sort-array');
const data = [{ name: 'John', age: 30 }, { name: 'Jane', age: 25 }, { name: 'Jim', age: 35 }];
const sortedData = sortArray(data, { by: ['age', 'name'] });
console.log(sortedData);

Sort in Descending Order

This feature allows you to sort an array of objects in descending order. In this example, the array is sorted by the 'age' field in descending order.

const sortArray = require('sort-array');
const data = [{ name: 'John', age: 30 }, { name: 'Jane', age: 25 }, { name: 'Jim', age: 35 }];
const sortedData = sortArray(data, { by: 'age', order: 'desc' });
console.log(sortedData);

Custom Comparator Function

This feature allows you to use a custom comparator function to sort the array. In this example, the array of numbers is sorted in descending order using a custom comparator function.

const sortArray = require('sort-array');
const data = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5];
const sortedData = sortArray(data, { comparator: (a, b) => b - a });
console.log(sortedData);

Other packages similar to sort-array

Keywords

FAQs

Package last updated on 29 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

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc