Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
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 sort-on
import sortOn from '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` property, 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'}], element => element.x);
//=> [{x: 'a'}, {x: 'b'}, {x: 'c'}]
Returns a new sorted version of the given array.
Type: unknown[]
The array to sort.
Type: string | string[] | Function
The string can be a dot path to a nested object property.
Prefix it with -
to sort it in descending order.
Type: object
Type: string | string[]
Default: The default locale of the JavaScript runtime.
One or more locales to use when sorting strings.
Should be a locale string or array of locale strings that contain one or more language or locale tags.
If you include more than one locale string, list them in descending order of priority so that the first entry is the preferred locale.
This parameter must conform to BCP 47 standards. See Intl.Collator
for more details.
Type: Intl.CollatorOptions
Comparison options.
FAQs
Sort an array on an object property
The npm package sort-on receives a total of 237,252 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
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.