
Security News
New CVE Forecasting Tool Predicts 47,000 Disclosures in 2025
CVEForecast.org uses machine learning to project a record-breaking surge in vulnerability disclosures in 2025.
Sort arrays of objects or dictionaries of objects by property (including nested properties).
A handy utility function to sort arrays of objects or dictionaries of objects by property (including nested properties).
Sorting behaviour is the same as Array.sort()
. See the /examples
directory for some working examples.
You can sort an array of objects based on one of its (nested) properties. Arrays are sorted in place (the original array is mutated) and the result is returned.
const deepSort = require(`deep-sort`);
const myArray = [
{ id: 1, nested: { time: 111, deeper: { text: `AAA` } } },
{ id: 2, nested: { time: 222, deeper: { text: `BBB` } } },
{ id: 3, nested: { time: 333, deeper: { text: `CCC` } } },
];
// Shorthand.
deepSort.array(myArray, `id`); // Default is order ASC.
deepSort.array(myArray, `id`, `asc`);
deepSort.array(myArray, `id`, `desc`);
deepSort.array(myArray, `nested.time`, `asc`);
deepSort.array(myArray, `nested.deeper.text`, `desc`);
deepSort.array(myArray, `nested.deeper.text`, `desc`, comparatorFunction);
// Longhand (all arguments required).
deepSort(myArray, null, `id`, `asc`);
deepSort(myArray, null, `id`, `asc`, comparatorFunction);
You can sort a dictionary of objects based on one of its (nested) properties. Dictionaries are not mutated, and a new dictionary will be returned.
const deepSort = require(`deep-sort`);
const myDictionary = {
'key_1nd': { key: `key_1nd`, quantity: 98, nested: { time: 111, deeper: { text: `AAA` } } },
'key_k8a': { key: `key_k8a`, quantity: 45, nested: { time: 222, deeper: { text: `BBB` } } },
'key_aj3': { key: `key_aj3`, quantity: 1, nested: { time: 333, deeper: { text: `CCC` } } },
};
// Shorthand.
deepSort.object(myDictionary, `key`, `quantity`); // Default is order ASC.
deepSort.object(myDictionary, `key`, `quantity`, `asc`);
deepSort.object(myDictionary, `key`, `quantity`, `desc`);
deepSort.object(myDictionary, `key`, `nested.time`, `asc`);
deepSort.object(myDictionary, `key`, `nested.deeper.text`, `desc`);
deepSort.object(myDictionary, `key`, `nested.deeper.text`, `desc`, comparatorFunction);
// Longhand (all arguments required).
deepSort(myDictionary, `key`, `quantity`, `quantity`, `asc`);
deepSort(myDictionary, `key`, `quantity`, `quantity`, `asc`, comparatorFunction);
If you need more control you can pass a comparator function to the deepSort.custom()
method. You must return a numerical value from this function which can be accepted by Array.sort()
:
// Arrays.
deepSort.custom(array, resources => {
// resources.itemA -> the next item in the iterable.
// resources.itemB -> the next + 1 item in the iterable.
// resources.iterable -> the input iterable (array).
return an integer;
});
// Objects.
deepSort.custom(dictionary, `someKeyProperty`, resources => {
// resources.itemA -> the next item in the iterable.
// resources.itemB -> the next + 1 item in the iterable.
// resources.iterable -> the input iterable (dictionary).
return an integer;
});
If you pass a comparator function to deepSort()
, deepSort.array()
or deepSort.object()
you get access to all the input arguments:
function comparator (resources) {
// resources.propA -> the next item property in the iterable (specified by sortProperty).
// resources.propB -> the next + 1 item property in the iterable (specified by sortProperty).
// resources.itemA -> the next item in the iterable.
// resources.itemB -> the next + 1 item in the iterable.
// resources.iterable -> the input iterable (array or dictionary).
// resources.keyProperty -> the input keyProperty (array or dictionary).
// resources.sortProperty -> the input sortProperty (array or dictionary).
// resources.sortDirection -> the input sortDirection (array or dictionary).
return an integer;
}
Sort the given iterable (array or dictionary) with all arguments available.
Sort the given array using the given object path, ordering flag, and optional comparator.
The optional comparator function will be passed the following values (e.g. comparator (resources) { ... }
):
Sort the given object using the given key property, object path and custom comparator function.
The optional comparator function will be passed the following values (e.g. comparator (resources) { ... }
):
Sort the given iterable using the result of a custom comparator function.
The required comparator function will be passed the following values (e.g. comparator (resources) { ... }
):
FAQs
Sort arrays of objects or dictionaries of objects by property (including nested properties).
The npm package deep-sort receives a total of 21 weekly downloads. As such, deep-sort popularity was classified as not popular.
We found that deep-sort demonstrated a not healthy version release cadence and project activity because the last version was released 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
CVEForecast.org uses machine learning to project a record-breaking surge in vulnerability disclosures in 2025.
Security News
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
Research
Security News
Eight new malicious Firefox extensions impersonate games, steal OAuth tokens, hijack sessions, and exploit browser permissions to spy on users.