Last active
October 11, 2015 05:37
-
-
Save shinnn/3810690 to your computer and use it in GitHub Desktop.
Sort an array of objects by multiple fields
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ref: http://ituneslp.net/tutorials/itunescontrol | |
array.sortOn = function() { | |
for(var i=0 ; i < arguments.length; i++){ | |
var field = arguments[i]; | |
this.sort(function(a, b){ | |
if(a[field] > b[field]){ | |
return 1; | |
} | |
if(a[field] < b[field]){ | |
return -1; | |
} | |
return 0; | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment