Last active
August 29, 2015 13:55
-
-
Save justinvdm/8785751 to your computer and use it in GitHub Desktop.
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
var data = [ | |
{foo: 1}, | |
{foo: 2}]; | |
// if you have objects with the same `foo` value, their current ordering with | |
// respect to each other *will not* be preserved. | |
data.sort(function(a, b) { | |
return a.foo < b.foo | |
? 1 | |
: 0; | |
}); | |
// if you have objects with the same `foo` value, their current ordering with | |
// respect to each other *should* be preserved. | |
data.sort(function(a, b) { | |
if (a.foo < b.foo) { | |
return -1; | |
} | |
else if (a.foo > b.foo) { | |
return 1; | |
} | |
else { | |
return 0; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment