Last active
December 15, 2015 08:59
-
-
Save davidlonjon/5235032 to your computer and use it in GitHub Desktop.
JavaScript: Dynamic Sort
This file contains 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
// Taken from http://stackoverflow.com/questions/1129216/sorting-objects-in-an-array-by-a-field-value-in-javascript | |
dynamic_sort = function(property) { | |
var sort_order = 1; | |
if(property[0] === "-") { | |
sort_order = -1; | |
property = property.substr(1, property.length - 1); | |
} | |
return function (a,b) { | |
var result = (a[property] < b[property]) ? -1 : (a[property] > b[property]) ? 1 : 0; | |
return result * sort_order; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment