Extjs - Store sorting by alphanumeric and case-sensitive

I want to sort the store (ArrayStore and GroupingStore) in alphabetical order and in the case of INSENSITIVELY. I use the singleSort () method, but it sorts the case sensitively.

For example,

data = ['c', '1', 'A', 'a', 'C'] output = ['1', 'A', 'C', 'a', 'c'] myoutput = ['1', 'a', 'A', 'c', 'C'] or [['1', 'A', 'a', 'C', 'c'] // This is what I want 

Any suggestion on how to achieve this?

+6
extjs store
source share
4 answers

Add this to your code ... be careful with your areas, because you will be doing all kinds of pages on a case insensitive basis. I found this code in the forums and successfully used it in 3.2.1.

 Ext.data.Store.prototype.sortData = function(f, direction){ direction = direction || 'ASC'; var st = this.fields.get(f).sortType; var fn = function(r1, r2) { var v1 = st(r1.data[f]), v2 = st(r2.data[f]); // ADDED THIS FOR CASE INSENSITIVE SORT if (v1.toLowerCase) { v1 = v1.toLowerCase(); v2 = v2.toLowerCase(); } return v1 > v2 ? 1 : (v1 < v2 ? -1 : 0); }; this.data.sort(direction, fn); if (this.snapshot && this.snapshot != this.data) { this.snapshot.sort(direction, fn); } 

}

+6
source share

I found a much easier way

Using ExtJS 3.3.1, it may work with earlier.

Just define sortType for the asUCString field, for example:

 new Ext.data.JsonStore({ url: 'my.php', fields: [{name:'name', sortType:Ext.data.SortTypes.asUCString}], sortInfo: { field: 'name', direction: 'ASC'} }); 
+19
source share

For 3.2.0, I had to override the createSortFunction function as follows:

 Ext.override(Ext.data.Store, { // override createSortFunction : function(field, direction) { direction = direction || "ASC"; var directionModifier = direction.toUpperCase() == "DESC" ? -1 : 1; var sortType = this.fields.get(field).sortType; //create a comparison function. Takes 2 records, returns 1 if record 1 is greater, //-1 if record 2 is greater or 0 if they are equal return function(r1, r2) { var v1 = sortType(r1.data[field]), v2 = sortType(r2.data[field]); // To perform case insensitive sort if (v1.toLowerCase) { v1 = v1.toLowerCase(); v2 = v2.toLowerCase(); } return directionModifier * (v1 > v2 ? 1 : (v1 < v2 ? -1 : 0)); }; } }); 

Hope this helps someone.

+6
source share

transform: function (displayName) {return displayName.toLowerCase (); }

 store: fields: ['value', 'displayName'], data: [], sorters: [{ property: 'displayName', direction: 'ASC', transform: function (displayName) { return displayName.toLowerCase(); } }] 
+5
source share

All Articles