Sorts any JavaScript array in a predictable way (deep equal arrays are always sorted in the same order)
Installations
npm install sort-any
Developer Guide
Typescript
No
Module System
CommonJS
Min. Node Version
>=12
Node Version
14.21.3
NPM Version
7.7.6
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Love this project? Help keep it running — sponsor us today! 🚀
Developer
oprogramador
Download Statistics
Total Downloads
37,969,003
Last Day
100,360
Last Week
552,147
Last Month
2,181,023
Last Year
20,702,115
GitHub Statistics
4 Stars
200 Commits
3 Watching
6 Branches
4 Contributors
Bundle Size
71.53 kB
Minified
25.02 kB
Minified + Gzipped
Package Meta Information
Latest Version
4.0.6
Package Id
sort-any@4.0.6
Unpacked Size
10.22 kB
Size
3.14 kB
File Count
3
NPM Version
7.7.6
Node Version
14.21.3
Publised On
07 Apr 2024
Total Downloads
Cumulative downloads
Total Downloads
37,969,003
Last day
2.4%
100,360
Compared to previous day
Last week
21.8%
552,147
Compared to previous week
Last month
30.7%
2,181,023
Compared to previous month
Last year
257.2%
20,702,115
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
1
Dev Dependencies
22
sort-any
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
:
1[[[11]],[[2]],2,{foo:{foo:2}},1,4,2,{foo:{foo:1}},32,[3,4],[1,2],{foo:1},{foo:0},{foo:{}}].sort() 2/* 3It gives: 4[ 1, 5 [ 1, 2 ], 6 [ [ 11 ] ], 7 2, 8 2, 9 [ [ 2 ] ], 10 [ 3, 4 ], 11 32, 12 4, 13 { foo: { foo: 2 } }, 14 { foo: {} }, 15 { foo: 1 }, 16 { foo: 0 }, 17 { foo: { foo: 1 } } ] 18*/
The same array in a different order:
1[[[11]],[[2]],2,1,4,2,{foo:{foo:1}},32,[3,4],[1,2],{foo:0},{foo:1},{foo:{}},{foo:{foo:2}}].sort() 2/* 3It gives: 4[ 1, 5 [ 1, 2 ], 6 [ [ 11 ] ], 7 2, 8 2, 9 [ [ 2 ] ], 10 [ 3, 4 ], 11 32, 12 4, 13 { foo: { foo: 1 } }, 14 { foo: 0 }, 15 { foo: 1 }, 16 { foo: {} }, 17 { foo: { foo: 2 } } ] 18*/
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:
1const sort = require('sort-any'); 2sort([[[11]],[[2]],2,{foo:{foo:2}},1,4,2,{foo:{foo:1}},32,[3,4],[1,2],{foo:1},{foo:0},{foo:{}}]) 3/* 4It returns: 5[ 1, 6 2, 7 2, 8 4, 9 32, 10 [ [ 2 ] ], 11 [ [ 11 ] ], 12 [ 1, 2 ], 13 [ 3, 4 ], 14 { foo: 0 }, 15 { foo: 1 }, 16 { foo: {} }, 17 { foo: { foo: 1 } }, 18 { foo: { foo: 2 } } ] 19*/
And when we change the order, the result remains the same.
1const sort = require('sort-any'); 2sort([[[11]],[[2]],2,1,4,2,{foo:{foo:1}},32,[3,4],[1,2],{foo:0},{foo:1},{foo:{}},{foo:{foo:2}}]) 3/* 4It returns: 5[ 1, 6 2, 7 2, 8 4, 9 32, 10 [ [ 2 ] ], 11 [ [ 11 ] ], 12 [ 1, 2 ], 13 [ 3, 4 ], 14 { foo: 0 }, 15 { foo: 1 }, 16 { foo: {} }, 17 { foo: { foo: 1 } }, 18 { foo: { foo: 2 } } ] 19*/
Rules for sorting:
- the most important is the type (from the smallest to the largest):
- undefined
- null
- boolean
- NaN
- number (all the numbers except of NaN)
- string
- symbol
- date
- set
- array
- map
- object (all the objects except of arrays, dates and null)
false
is less thantrue
- numbers are sorted with the standard numeric order
-Infinity
is less than any other numberInfinity
is more than any other number- strings are sorted in the alphabetical order
- symbols are sorted in the alphabetical order according to their description
- dates are sorted in the chronological order
- rules of arrays sorting:
- the most important is the length (always a shorter array is less than a longer array)
- if the length is the same, we sort (recursively using this algorithm) both arrays and we compare the smallest item from both arrays
- if the smallest item is the same, we compare the second smallest item, and so on
- if the arrays include the same values, we compare the elements at 0 index
- if the elements at 0 index are the same, we compare the elements at 1 index, 2 index, and so on
- if all the elements are equal, the arrays are equal
- rules of objects sorting:
- the most important is the number of the keys (always an object with less keys is less than an object with more keys)
- if the number of keys is the same, we sort (recursively using this algorithm) the keys (which can be either strings or symbols) and we compare the smallest keys from both objects
- if the smallest key is the same, we compare the second smallest key, and so on
- if all the keys are the same, we compare the values at the smallest key
- if the values at the smallest key are the same, we compare sequentially next values taking each time a bigger key unless the values differ
- if all the values are the same, we compare the keys at 0 index (
Object.keys(object)[0]
) - if the keys at 0 index are the same, we compare the keys at 1 index, 2 index, and so on
- if all the keys are the same, the objects are equal
- rules of sets sorting:
- the set is converted to an array and the array sorting rules apply
- rules of maps sorting:
- the map is converted to an object (with the correspondent keys and values) and the object sorting rules apply
![Empty State](/_next/static/media/empty.e5fae2e5.png)
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
Found 0/17 approved changesets -- score normalized to 0
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
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
license file not detected
Details
- Warn: project does not have a license file
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
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 13 are checked with a SAST tool
Reason
38 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-2h3h-q99f-3fhc
- Warn: Project is vulnerable to: GHSA-gmw6-94gg-2rc2
- Warn: Project is vulnerable to: GHSA-hxwm-x553-x359
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-cwfw-4gq5-mrqx
- Warn: Project is vulnerable to: GHSA-g95f-p29q-9xw4
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c
- Warn: Project is vulnerable to: GHSA-8r6j-v8pm-fqw3
- Warn: Project is vulnerable to: MAL-2023-462
- Warn: Project is vulnerable to: GHSA-4q6p-r6v2-jvc5
- Warn: Project is vulnerable to: GHSA-ww39-953v-wcq6
- Warn: Project is vulnerable to: GHSA-78xj-cgh5-2h22
- Warn: Project is vulnerable to: GHSA-2p57-rm9w-gvfp
- Warn: Project is vulnerable to: GHSA-896r-f27r-55mw
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-6c8f-qphg-qjgp
- Warn: Project is vulnerable to: GHSA-29mw-wpgm-hmr9
- Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-qrpm-p2h7-hrv2
- Warn: Project is vulnerable to: GHSA-mwcw-c2x4-8c55
- Warn: Project is vulnerable to: GHSA-9wv6-86v2-598j
- Warn: Project is vulnerable to: GHSA-p8p7-x288-28g6
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-4g88-fppr-53pp
- Warn: Project is vulnerable to: GHSA-4jqc-8m5r-9rpr
- Warn: Project is vulnerable to: GHSA-f5x3-32g6-xq36
- Warn: Project is vulnerable to: GHSA-3jfq-g458-7qm9
- Warn: Project is vulnerable to: GHSA-r628-mhmh-qjhw
- Warn: Project is vulnerable to: GHSA-9r2w-394v-53qc
- Warn: Project is vulnerable to: GHSA-5955-9wpr-37jh
- Warn: Project is vulnerable to: GHSA-qq89-hq3f-393p
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
Score
1.3
/10
Last Scanned on 2025-02-03
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 MoreOther packages similar to sort-any
array-sort
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.
gulp-sort
Sort files in stream by path or any custom sort comparator
sort-array
Isomorphic, load-anywhere function to sort an array by scalar, deep or computed values in any standard or custom order
sort-es
Blazing fast, tree-shakeable, type-safe, modern utility library to sort any type of array in less than 1 KB!