I am working on a backbone application at the moment, and I wanting to order my collection based on various attributes, I have sorting working based on cost, duration, start and finish date (timestamps) but I am struggling to sort the collection based on the name attribute, here is my collection code,
var ProjectCollection = Backbone.Collection.extend({
url: '/projects',
model: app.Project,
sort_key: "finish_date",
sort_order: 1,
parent_filter: false,
initialize: function() {
var pm = this.get('projectmanager');
this.set("project_manager", new app.User(pm));
var sp = this.get('salesperson');
this.set("sales_person", new app.User(sp));
},
comparator: function (item1, item2) {
return (item1.get(this.sort_key) - item2.get(this.sort_key)) * this.sort_order;
},
sortByField: function(fieldName, orderType) {
this.sort_key = fieldName;
this.sort_order = orderType == "desc" ? -1 : 1;
console.log(this.sort_order);
this.sort();
},
});
Is there are way to sort alphabetically on a string?
I have added the new comparator function, and this is output I get when ordering via name in an ascending fashion,
Android Application
Hello World I am a new instance of a project
Java Application
Simon's Project
Some new project
Symfony App,
Windows Application
iPhone Application
As you can see iPhone application should not be last in the list.