Node.js - sorto
Sort an object by key or value.
Why?
When I'm crunching through data, I like to keep a count on various strings using an associative array. It's handy to be able to view this data sorted alphabetically or by count.
Installation
npm install --save sorto
Methods
sorto.bykey(collection, [transformFunc])
Sorts an object or array of single keyed objects by key.
Examples:
var sorto = var obj = 'b': 'hi' 'cat': 'meow' 'zoo': 'animals' 'jordan': 'basketball' 'arthur': 'aardvark' 'tim': 'person' var items = sorto //use a transform functionvar obj = '7': 'hi' '90.5': 'meow' '-5': 'animals' '33.2': 'basketball' '0': 'aardvark' '4': 'person' var items = sorto
sorto.byval(collection, [transformFunc])
Sorts an object or array of single keyed objects by value.
Examples:
var sorto = var obj = 'b': 'hi' 'cat': 'meow' 'zoo': 'animals' 'jordan': 'basketball' 'arthur': 'aardvark' 'tim': 'person' var items = sorto //use transform functionvar obj = 'b': '7' 'cat': '90.5' 'zoo': '-5' 'jordan': '33.2' 'arthur': '0' 'tim': '4' var items = sortoconsolediritems
Utility Methods
Since sorto
returns an array of objects with a single/key value pair, it can be kind of annoying accessing those values if you don't know
their names.
k(obj)
Get the key of an object with a single key/value pair.
Example:
var obj = name: 'jp'
v(obj)
Get the value of an object with a single key/value pair.
Example:
var obj = name: 'jp'
License
(MIT License)
Copyright 2013 JP Richardson jprichardson@gmail.com