
Security News
CISA Kills Off RSS Feeds for KEVs and Cyber Alerts
CISA is discontinuing official RSS support for KEV and cybersecurity alerts, shifting updates to email and social media, disrupting automation workflows.
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)
The npm package sort-any receives a total of 486,335 weekly downloads. As such, sort-any popularity was classified as popular.
We found that sort-any 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
CISA is discontinuing official RSS support for KEV and cybersecurity alerts, shifting updates to email and social media, disrupting automation workflows.
Security News
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.
Research
Security News
Socket uncovers an npm Trojan stealing crypto wallets and BullX credentials via obfuscated code and Telegram exfiltration.