Skip to content

Instantly share code, notes, and snippets.

@justinvdm
Last active August 29, 2015 13:55
Show Gist options
  • Save justinvdm/8785751 to your computer and use it in GitHub Desktop.
Save justinvdm/8785751 to your computer and use it in GitHub Desktop.
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