0.0.2 • Published 8 years ago

sortBy.js v0.0.2

Weekly downloads
4
License
MIT
Repository
github
Last release
8 years ago

sortBy.js

originate from http://phrogz.net/JS/Array.prototype.sortBy.js

Install

npm install sortBy.js --save

Usage

Example 1:

var a=[ {c:"GK",age:37}, {c:"ZK",age:13}, {c:"TK",age:14}, {c:"AK",age:13} ];    
a.sortBy( function(){ return this.age } );                                       
  --> [ {c:"ZK",age:13}, {c:"AK",age:13}, {c:"TK",age:14}, {c:"GK",age:37} ]     
a.sortBy( function(){ return [this.age,this.c] } );                              
  --> [ {c:"AK",age:13}, {c:"ZK",age:13}, {c:"TK",age:14}, {c:"GK",age:37} ]     
a.sortBy( function(){ return -this.age } );                                      
  --> [ {c:"GK",age:37}, {c:"TK",age:14}, {c:"ZK",age:13}, {c:"AK",age:13} ]     

Example 2:

var n=[ 1, 99, 15, "2", "100", 3, 34, "foo", "bar" ];                            
n.sort();                                                                        
  --> [ 1, "100", 15, "2", 3, 34, 99, "bar", "foo" ]                             
n.sortBy( function(){ return this*1 } );                                         
  --> [ "foo", "bar", 1, "2", 3, 15, 34, 99, "100" ]                             
n.sortBy( function(o){ return [typeof o,this] } );                               
  --> [1, 3, 15, 34, 99, "100", "2", "bar", "foo"]                               
n.sortBy(function(o){ return [typeof o, typeof o=="string" ? o.length : o] })    
  --> [1, 3, 15, 34, 99, "2", "100", "bar", "foo"]            
0.0.2

8 years ago

0.0.1

8 years ago