Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone sort method

I have the following code :

Campaign.Collection = Backbone.Collection.extend({initialize: function() {
},
comparator: function(item) { return item.get('Name'); }
}

I call collection.sort() and seems to work and sorts models regarding Name field the problem is that gives higher priority to uppercase letters example ('Some test' < 'more test') is there a way to override the behavior ?

like image 280
Petran Avatar asked May 14 '13 14:05

Petran


People also ask

What is backbone in programming?

Backbone. js is a model view controller (MVC) Web application framework that provides structure to JavaScript-heavy applications. This is done by supplying models with custom events and key-value binding, views using declarative event handling and collections with a rich application programming interface (API).

Is Backbone JS still used?

Backbone. Backbone has been around for a long time, but it's still under steady and regular development. It's a good choice if you want a flexible JavaScript framework with a simple model for representing data and getting it into views.

Is backbone a MVC?

Backbone is a JavaScript MVC library and that's a key difference.

What is backbone JS model?

It is also known as the heart of the JavaScript application. Model contains dynamic data and its logic. It performs various types of action on the data like validation, conversion, computed properties, access control etc. 1.


2 Answers

The easiest fix is to simply do the following:

Campaign.Collection = Backbone.Collection.extend({
    initialize: function() {},
    comparator: function(item) { return item.get('Name').toLowerCase(); }
};

This will convert all to lower case before comparing so it will compare in a manner that ignores case.

like image 119
Victor Quinn Avatar answered Oct 14 '22 07:10

Victor Quinn


For a case insensitive comparison, use the native JS function toLowerCase:

Campaign.Collection = Backbone.Collection.extend({initialize: function() {
},
comparator: function(item) { return item.get('Name').toLowerCase(); }
}
like image 33
Alanyst Avatar answered Oct 14 '22 07:10

Alanyst