smart-deep-sort

1.0.2 • Public • Published

smart-deep-sort

Deep sort an object, no matter what the contents are.

CircleCI Coverage Status

Install

npm install smart-deep-sort

Usage

var sort = require("smart-deep-sort")
 
var mixedTypes = {
  primativeInt: 2,
  primativeString: "1",
  mixedArray: [
    {
      nestedObjName: "Nestle",
      abilities: ["rock", "and", "roll"]
    },
    [4, 1, 2, "two", "twenty-thousand"],
    "basicString"
  ]
}
 
var sortedMixedTypes = {
  mixedArray: [
    [1, 2, 4, "twenty-thousand", "two"],
    {
      abilities: ["and", "rock", "roll"],
      nestedObjName: "Nestle"
    },
    "basicString"
  ],
  primativeInt: 2,
  primativeString: "1"
}
 
var ret = sort(mixedTypes)
console.log(JSON.stringify(ret) === JSON.stringify(sortedMixedTypes))

The Rules

  • Objects fields are deep sorted by key using deep-sort-object
    • keys at all levels are sorted using default string Unicode code sort order
  • Arrays elements are sorted by type, ordered on the constructor name. Arrays come first then Booleans, etc.
    • Nested objects are sorted by using sorty to order them by keys and values.
    • All other nested object types are sorted by their contents using array-sort

Limitations

  • Cannot handle objects with undefined keys, they will probably be dropped from the resulting object.
  • Not optimized, I don't recommend using this as part of stream processing.
  • Does not handle Date types. The default string representation of the date will be used in comparisons.

Package Sidebar

Install

npm i smart-deep-sort

Weekly Downloads

1,093

Version

1.0.2

License

MIT

Unpacked Size

18.9 kB

Total Files

8

Last publish

Collaborators

  • collinear-group
  • kingnebby