bcn-compare

0.0.4 • Public • Published

bcn-compare

Provides comparator to fully sort any javascript data regardless of type

compare(a, b) -> -1 | 0 | 1

rules are:

undefined < null null < number number < string string < array array < object

  • for arrays of unequal length, out of bounds values are treated as undefined

  • objects are sorted by the following rules where:

    • keys whose value is undefined are treated as not existing
    • objects with fewer keys < objects with more keys
    • keys are checked in sorted order where first keys that are not identical determine sort order

Installation

npm install bcn-compare

Usage

var compare = require("bcn-compare").compare;
 
var a = ["test", {}, ["an", "array"], 4, 3, 2, {"hello": 3}];
 
a.sort(compare);
 

Readme

Keywords

none

Package Sidebar

Install

npm i bcn-compare

Weekly Downloads

1

Version

0.0.4

License

none

Last publish

Collaborators

  • markstewart