Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Sorts any JavaScript array in a predictable way (deep equal arrays are always sorted in the same order)
JS library which always sorts arrays in a predictable way. Moreover in contrary to Array.prototype.sort
, it does not modify the argument.
Array.prototype.sort
:
[[[11]],[[2]],2,{foo:{foo:2}},1,4,2,{foo:{foo:1}},32,[3,4],[1,2],{foo:1},{foo:0},{foo:{}}].sort()
/*
It gives:
[ 1,
[ 1, 2 ],
[ [ 11 ] ],
2,
2,
[ [ 2 ] ],
[ 3, 4 ],
32,
4,
{ foo: { foo: 2 } },
{ foo: {} },
{ foo: 1 },
{ foo: 0 },
{ foo: { foo: 1 } } ]
*/
The same array in a different order:
[[[11]],[[2]],2,1,4,2,{foo:{foo:1}},32,[3,4],[1,2],{foo:0},{foo:1},{foo:{}},{foo:{foo:2}}].sort()
/*
It gives:
[ 1,
[ 1, 2 ],
[ [ 11 ] ],
2,
2,
[ [ 2 ] ],
[ 3, 4 ],
32,
4,
{ foo: { foo: 1 } },
{ foo: 0 },
{ foo: 1 },
{ foo: {} },
{ foo: { foo: 2 } } ]
*/
So the results of Array.prototype.sort
are strange (eg. numbers are sorted in the alphabetical order) and moreover if we change the array order (eg. for object items), the result has order changed as well.
So I have implemented this library to work like that:
const sort = require('sort-any');
sort([[[11]],[[2]],2,{foo:{foo:2}},1,4,2,{foo:{foo:1}},32,[3,4],[1,2],{foo:1},{foo:0},{foo:{}}])
/*
It returns:
[ 1,
2,
2,
4,
32,
[ [ 2 ] ],
[ [ 11 ] ],
[ 1, 2 ],
[ 3, 4 ],
{ foo: 0 },
{ foo: 1 },
{ foo: {} },
{ foo: { foo: 1 } },
{ foo: { foo: 2 } } ]
*/
And when we change the order, the result remains the same.
const sort = require('sort-any');
sort([[[11]],[[2]],2,1,4,2,{foo:{foo:1}},32,[3,4],[1,2],{foo:0},{foo:1},{foo:{}},{foo:{foo:2}}])
/*
It returns:
[ 1,
2,
2,
4,
32,
[ [ 2 ] ],
[ [ 11 ] ],
[ 1, 2 ],
[ 3, 4 ],
{ foo: 0 },
{ foo: 1 },
{ foo: {} },
{ foo: { foo: 1 } },
{ foo: { foo: 2 } } ]
*/
Rules for sorting:
false
is less than true
-Infinity
is less than any other numberInfinity
is more than any other numberObject.keys(object)[0]
)FAQs
Sorts any JavaScript array in a predictable way (deep equal arrays are always sorted in the same order)
We found that sort-any 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
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.