0

im learning BackboneJs using the documentation and a book called "Beginning backbone". But I have been stuck at the sorting collections part for hours. Also tried to research but I find the results complicated =/

I know I have to use the comparator, as shown in the documentation but I don't understand how to apply it to the current code syntax-wise

http://backbonejs.org/#Collection-comparator

var Book = Backbone.Model.extend({
    defaults:
    {
        title: "default title",
        author: "default author",
        pages: 20
    },
    comparator: function(item)
    {
        //sort by title
        return item.get('title');   
    }
});

var book1 = new Book({ title:"Book of wonders",author:"author1",pages:1 });
var book2 = new Book({ title:"Zelda",author:"author2",pages:2 });
var book3 = new Book({ title: "Drake's out", author: "author3",pages:3});
var book4 = new Book({ title: "AutoCad",author: "author4",pages: 4});

var Library = Backbone.Collection.extend({
    model: Book
});

var library = new Library([book1,book2]);
library.add([book3,book4]); 

library.forEach(function(model){
    console.log('Book is called '+model.get("title"));
});

console.log('Library contains '+library.length+' books');
The Guy with The Hat
  • 10,836
  • 8
  • 57
  • 75
user254883
  • 599
  • 1
  • 5
  • 14

1 Answers1

0

This is a working solution, it will sort everything by title. to sort it by anything else just change the parameter of the get function inside the comparator function

    var Book = Backbone.Model.extend({
        defaults:
        {
            title: "default title",
            author: "default author",
            pages: 20
        } 
    });

    var book1 = new Book({ title:"Book of wonders",author:"author1",pages:1 });
    var book2 = new Book({ title:"Zelda",author:"author2",pages:2 });
    var book3 = new Book({ title: "Drake's out", author: "author3",pages:3});
    var book4 = new Book({ title: "AutoCad",author: "author4",pages: 4});

    var Library = Backbone.Collection.extend({
        model: Book,
        initialize: function()
        {
            console.log("new collection");
        },
        comparator: function(a,b)
        {
            //sort by title
            return a.get('title') < b.get('title') ? -1 : 1;
        }
    });

    var library = new Library([book1,book2]);
    library.add([book3,book4]); 

    library.sort(); 
    library.forEach(function(model){
        console.log('Book is called '+model.get("title"));
    });

    console.log('Library contains '+library.length+' books');   
user254883
  • 599
  • 1
  • 5
  • 14
  • You're missing the `a.get('title') == b.get('title')` case, your `comparator` should return zero in such cases as it ends up using the standard [`Array.prototype.sort`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) method. – mu is too short Mar 25 '14 at 16:52
  • do I have to put it at the first line of the comparator? – user254883 Mar 26 '14 at 08:51
  • 1
    You just need to cover three cases: `a_title == b_title`, `a_title < b_title`, `a_title > b_title`; those should return 0, -1, and 1 (respectively). Arrange the conditions however you want. – mu is too short Mar 26 '14 at 17:26