Skip to content

Instantly share code, notes, and snippets.

@jpillora
Created June 13, 2015 11:12
Show Gist options
  • Save jpillora/21481d73cfd489bb7c19 to your computer and use it in GitHub Desktop.
Save jpillora/21481d73cfd489bb7c19 to your computer and use it in GitHub Desktop.
parse collection comparator
//comparator string doesn't work, so you need to provide a sort function,
// read more http://mdn.io/sort
var Foo = Parse.Object.extend("Foo");
var Foos = Parse.Collection.extend("Foos", {
model: Foo
});
var foos = new Foos();
foos.add({
name: "A",
hello: 1,
world: 7
});
foos.add({
name: "B",
hello: 3,
world: 6
});
foos.add({
name: "C",
hello: 2,
world: 5
});
foos.comparator = function(a,b) {
//where 1 means move right (to the end)
//and -1 means move left (to the beginning)
if(a.get("hello") > b.get("hello")) {
return 1;
}
return -1;
};
foos.sort();
foos.each(function(foo) {
console.log(foo.get("name"));
});
// A
// C
// B
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment