Created
June 13, 2015 11:12
-
-
Save jpillora/21481d73cfd489bb7c19 to your computer and use it in GitHub Desktop.
parse collection comparator
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
//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