Skip to content
Browse files

Rename `sort` to `sortBy` and add the ability to specify an attribute…

… name as well as a custom function. Add `sort` method that acts on a collection just like `Array#sort`.
  • Loading branch information...
1 parent 3d1946d commit d8932a5ba2cade24c0df34142398d35cea237e74 @benpickles committed
Showing with 36 additions and 15 deletions.
  1. +1 −0 CHANGELOG.markdown
  2. +21 −3 src/model_class_methods.js
  3. +14 −12 test/public/tests/model_class_methods.js
View
1 CHANGELOG.markdown
@@ -1,5 +1,6 @@
# Changelog
+* Rename `sort` to `sortBy` and add the ability to specify an attribute name as well as a custom function. Add `sort` method that acts on a collection just like `Array#sort`.
* Add `pluck` method that takes an attribute name and returns an array of values.
* Fix for callbacks being wrongly called on multiple instances - they were being stored on the prototype and thus being shared across instances. Thanks to Oliver Nightingale for identifying the bug and writing a test case.
View
24 src/model_class_methods.js
@@ -92,9 +92,27 @@ Model.ClassMethods = {
},
sort: function(func) {
- var sorted = _.sortBy(this.all(), function(model, i) {
- return func.call(model, i);
- });
+ var sorted = this.all().slice().sort(func)
return this.chain(sorted);
+ },
+
+ sortBy: function(attribute_or_func) {
+ var is_func = jQuery.isFunction(attribute_or_func)
+ var extract = function(model) {
+ return attribute_or_func.call(model)
+ }
+
+ return this.sort(function(a, b) {
+ var a_attr = is_func ? extract(a) : a.attr(attribute_or_func)
+ var b_attr = is_func ? extract(b) : b.attr(attribute_or_func)
+
+ if (a_attr < b_attr) {
+ return -1
+ } else if (a_attr > b_attr) {
+ return 1
+ } else {
+ return 0
+ }
+ })
}
};
View
26 test/public/tests/model_class_methods.js
@@ -158,26 +158,28 @@ test(".pluck", function() {
test("sort (and chaining)", function() {
var Post = Model('post');
- var post1 = new Post({ title: "bcd" });
- var post2 = new Post({ title: "xyz" });
- var post3 = new Post({ title: "Acd" });
- var post4 = new Post({ title: "abc" });
+ var post1 = new Post({ number: 4, title: "bcd" });
+ var post2 = new Post({ number: 3, title: "xyz" });
+ var post3 = new Post({ number: 2, title: "Acd" });
+ var post4 = new Post({ number: 1, title: "abc" });
Post.add(post1, post2, post3, post4);
- same(Post.all(), [post1, post2, post3, post4]);
+ same(Post.pluck("title"), ["bcd", "xyz", "Acd", "abc"])
- same(Post.sort(function() {
- return this.attr("title").toLowerCase();
- }).all(), [post4, post3, post1, post2]);
+ same(Post.sortBy("title").pluck("title"), ["Acd", "abc", "bcd", "xyz"])
same(Post.select(function() {
return this.attr("title").indexOf("c") > -1;
- }).sort(function() {
- return this.attr("title").toLowerCase();
- }).all(), [post4, post3, post1]);
+ }).sortBy(function() {
+ return this.attr("title").toLowerCase()
+ }).pluck("title"), ["abc", "Acd", "bcd"])
- same(Post.all(), [post1, post2, post3, post4],
+ same(Post.sort(function(a, b) {
+ return a.attr("number") - b.attr("number")
+ }).pluck("title"), ["abc", "Acd", "xyz", "bcd"])
+
+ same(Post.pluck("title"), ["bcd", "xyz", "Acd", "abc"],
"original collection should be untouched");
});

0 comments on commit d8932a5

Please sign in to comment.
Something went wrong with that request. Please try again.