npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sort-any

v4.0.6

Published

Sorts any JavaScript array in a predictable way (deep equal arrays are always sorted in the same order)

Downloads

2,151,656

Readme

sort-any

MIT License Build Status

NPM status

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:

  • 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 than true
  • numbers are sorted with the standard numeric order
  • -Infinity is less than any other number
  • Infinity 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