Security News
Introducing the Socket Python SDK
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
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 380,857 weekly downloads. As such, sort-any popularity was classified as popular.
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
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
Security News
Floating dependency ranges in npm can introduce instability and security risks into your project by allowing unverified or incompatible versions to be installed automatically, leading to unpredictable behavior and potential conflicts.
Security News
A new Rust RFC proposes "Trusted Publishing" for Crates.io, introducing short-lived access tokens via OIDC to improve security and reduce risks associated with long-lived API tokens.