Fast and powerful array sorting. Sort an array of objects by one or more properties. Any number of nested properties or custom comparison functions may be used.
Installations
npm install array-sort
Developer Guide
Typescript
No
Module System
CommonJS
Min. Node Version
>=0.10.0
Node Version
8.4.0
NPM Version
5.3.0
Score
99.3
Supply Chain
99.2
Quality
77.4
Maintenance
100
Vulnerability
100
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Developer
jonschlinkert
Download Statistics
Total Downloads
381,119,202
Last Day
200,550
Last Week
916,376
Last Month
4,201,089
Last Year
62,248,286
GitHub Statistics
74 Stars
28 Commits
24 Forks
4 Watching
2 Branches
5 Contributors
Bundle Size
2.96 kB
Minified
1.15 kB
Minified + Gzipped
Package Meta Information
Latest Version
1.0.0
Package Id
array-sort@1.0.0
Size
4.23 kB
NPM Version
5.3.0
Node Version
8.4.0
Publised On
06 Dec 2017
Total Downloads
Cumulative downloads
Total Downloads
381,119,202
Last day
-5.6%
200,550
Compared to previous day
Last week
-14.6%
916,376
Compared to previous week
Last month
3.4%
4,201,089
Compared to previous month
Last year
-16.6%
62,248,286
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
3
Dev Dependencies
7
array-sort
![Windows Build Status](https://img.shields.io/appveyor/ci/jonschlinkert/array-sort.svg?style=flat&label=AppVeyor)
Fast and powerful array sorting. Sort an array of objects by one or more properties. Any number of nested properties or custom comparison functions may be used.
Install
Install with npm:
1$ npm install --save array-sort
Install with yarn:
1$ yarn add array-sort
Usage
Sort an array by the given object property:
1var arraySort = require('array-sort'); 2 3arraySort([{foo: 'y'}, {foo: 'z'}, {foo: 'x'}], 'foo'); 4//=> [{foo: 'x'}, {foo: 'y'}, {foo: 'z'}]
Reverse order
1arraySort([{foo: 'y'}, {foo: 'z'}, {foo: 'x'}], 'foo', {reverse: true}); 2//=> [{foo: 'z'}, {foo: 'y'}, {foo: 'x'}]
Params
1arraySort(array, comparisonArgs);
array
: {Array} The array to sortcomparisonArgs
: {Function|String|Array}: One or more functions or object paths to use for sorting.
Examples
1var arraySort = require('array-sort'); 2 3var posts = [ 4 { path: 'c.md', locals: { date: '2014-01-09' } }, 5 { path: 'a.md', locals: { date: '2014-01-02' } }, 6 { path: 'b.md', locals: { date: '2013-05-06' } }, 7]; 8 9// sort by `locals.date` 10console.log(arraySort(posts, 'locals.date')); 11 12// sort by `path` 13console.log(arraySort(posts, 'path'));
1var arraySort = require('array-sort'); 2 3var posts = [ 4 { locals: { foo: 'bbb', date: '2013-05-06' }}, 5 { locals: { foo: 'aaa', date: '2012-01-02' }}, 6 { locals: { foo: 'ccc', date: '2014-01-02' }}, 7 { locals: { foo: 'ccc', date: '2015-01-02' }}, 8 { locals: { foo: 'bbb', date: '2014-06-01' }}, 9 { locals: { foo: 'aaa', date: '2014-02-02' }}, 10]; 11 12// sort by `locals.foo`, then `locals.date` 13var result = arraySort(posts, ['locals.foo', 'locals.date']); 14 15console.log(result); 16// [ { locals: { foo: 'aaa', date: '2012-01-02' } }, 17// { locals: { foo: 'aaa', date: '2014-02-02' } }, 18// { locals: { foo: 'bbb', date: '2013-05-06' } }, 19// { locals: { foo: 'bbb', date: '2014-06-01' } }, 20// { locals: { foo: 'ccc', date: '2014-01-02' } }, 21// { locals: { foo: 'ccc', date: '2015-01-02' } } ]
If custom functions are supplied, array elements are sorted according to the return value of the compare function. See the docs for Array.sort()
for more details.
1var arr = [ 2 {one: 'w', two: 'b'}, 3 {one: 'z', two: 'a'}, 4 {one: 'x', two: 'c'}, 5 {one: 'y', two: 'd'}, 6]; 7 8function compare(prop) { 9 return function (a, b) { 10 return a[prop].localeCompare(b[prop]); 11 }; 12} 13 14var result = arraySort(arr, function (a, b) { 15 return a.two.localeCompare(b.two); 16}); 17 18console.log(result); 19// [ { one: 'z', two: 'a' }, 20// { one: 'w', two: 'b' }, 21// { one: 'x', two: 'c' }, 22// { one: 'y', two: 'd' } ]
1var arr = [ 2 {foo: 'w', bar: 'y', baz: 'w'}, 3 {foo: 'x', bar: 'y', baz: 'w'}, 4 {foo: 'x', bar: 'y', baz: 'z'}, 5 {foo: 'x', bar: 'x', baz: 'w'}, 6]; 7 8// reusable compare function 9function compare(prop) { 10 return function (a, b) { 11 return a[prop].localeCompare(b[prop]); 12 }; 13} 14 15// the `compare` functions can be a list or array 16var result = arraySort(arr, compare('foo'), compare('bar'), compare('baz')); 17 18console.log(result); 19// [ { foo: 'w', bar: 'y', baz: 'w' }, 20// { foo: 'x', bar: 'x', baz: 'w' }, 21// { foo: 'x', bar: 'y', baz: 'w' }, 22// { foo: 'x', bar: 'y', baz: 'z' } ]
About
Related projects
- get-value: Use property paths (
a.b.c
) to get a nested value from an object. | homepage - set-value: Create nested values and any intermediaries using dot notation (
'a.b.c'
) paths. | homepage - sort-asc: Sort array elements in ascending order. | homepage
- sort-desc: Sort array elements in descending order. | homepage
- sort-object: Sort the keys in an object. | homepage
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Contributors
Commits | Contributor |
---|---|
10 | jonschlinkert |
4 | doowb |
1 | iamstolis |
1 | wkevina |
Building docs
(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)
To generate the readme, run the following command:
1$ npm install -g verbose/verb#dev verb-generate-readme && verb
Running tests
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
1$ npm install && npm test
Author
Jon Schlinkert
License
Copyright © 2017, Jon Schlinkert. Released under the MIT License.
This file was generated by verb-generate-readme, v0.6.0, on September 11, 2017.
![Empty State](/_next/static/media/empty.e5fae2e5.png)
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
0 existing vulnerabilities detected
Reason
Found 3/20 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 12 are checked with a SAST tool
Score
3.2
/10
Last Scanned on 2025-01-27
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More