Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
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.
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);
Lodash is a popular utility library that provides a wide range of functions for manipulating arrays, objects, and other data structures. It includes a `_.sortBy` function that can be used to sort an array of objects by one or more properties. Compared to sort-on, Lodash offers a broader range of utilities but may be more complex to use for simple sorting tasks.
The array-sort package provides a simple way to sort arrays of objects by one or more properties. It is similar to sort-on in terms of functionality but may have a different API and fewer features. It is a lightweight alternative for basic sorting needs.
The sort-array package allows you to sort arrays of objects by one or more properties. It is similar to sort-on but offers additional customization options, such as specifying the sort order (ascending or descending) for each property. It is a good alternative if you need more control over the sorting process.
Sort an array on an object property
$ npm install --save sort-on
const sortOn = require('sort-on');
// sort by an object property
sortOn([{x: 'b'}, {x: 'a'}, {x: 'c'}], 'x');
//=> [{x: 'a'}, {x: 'b'}, {x: 'c'}]
// sort descending by an object property
sortOn([{x: 'b'}, {x: 'a'}, {x: 'c'}], '-x');
//=> [{x: 'c'}, {x: 'b'}, {x: 'a'}]
// sort by a nested object property
sortOn([{x: {y: 'b'}}, {x: {y: 'a'}}], 'x.y');
//=> [{x: {y: 'a'}}, {x: {y: 'b'}}]
// sort descending by a nested object property
sortOn([{x: {y: 'b'}}, {x: {y: 'a'}}], '-x.y');
//=> [{x: {y: 'b'}, {x: {y: 'a'}}}]
// sort by the `x` propery, then `y`
sortOn([{x: 'c', y: 'c'}, {x: 'b', y: 'a'}, {x: 'b', y: 'b'}], ['x', 'y']);
//=> [{x: 'b', y: 'a'}, {x: 'b', y: 'b'}, {x: 'c', y: 'c'}]
// sort by the returned value
sortOn([{x: 'b'}, {x: 'a'}, {x: 'c'}], el => el.x);
//=> [{x: 'a'}, {x: 'b'}, {x: 'c'}]
Returns a new sorted array.
Type: Array
Type: string
string[]
Function
The string can be a dot path to a nested object property. Prepend it with -
to sort it in descending order.
MIT © Sindre Sorhus
FAQs
Sort an array on an object property
The npm package sort-on receives a total of 172,368 weekly downloads. As such, sort-on popularity was classified as popular.
We found that sort-on demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
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.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.