Wednesday, December 28, 2011

Sorting store case-insensitively

The following code can be used to sort data in a store alphanumerically and case-insensitively.


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);
}
}

Note, this will cause all the stores to sort case-insensitively since we are overriding the default behavior.