PDA

View Full Version : Alpha Numeric Search in EXT JS



palchuri
26 Oct 2009, 8:02 AM
Alpha Numeric Sorting in EXTJS.


We have accounts FDX3201, FDX3202 and FDX19181.

Currently, if you sort ascending, it lists it in this order = FDX19181, FDX3201
& FDX3202. This is not correct. It should be looking at the entire number to
determine the order, not just the first. Given that 19181 is a higher number
than 3201 and 3202, this should be last on the list.

Any suggestions to achieve the desired behavior?

Thanks in advance.

Palchuri

Animal
26 Oct 2009, 8:39 AM
Sorting what?

jburnhams
26 Oct 2009, 9:23 AM
If sorting a store, you could create a custom sortType for that record's field (http://www.extjs.com/deploy/dev/docs/?class=Ext.data.Field).

nciportal
29 Oct 2009, 10:46 PM
override this API on headerclick event
Ext.override(Ext.data.Store, {
sortData : function(f, direction){
direction = direction || 'ASC';
var st = this.fields.get(f).sortType;
var fn = this.comparator || function(r1, r2){

var v1 = st(r1.data[f]), v2 = st(r2.data[f]);
v1 = v1.substr(v1.search("[0-9]"),v1.length);
v2 = v2.substr(v2.search("[0-9]"),v2.length);

return v1 > v2 ? 1 : (v1 < v2 ? -1 : 0);
};
this.data.sort(direction, fn, this);
if(this.snapshot && this.snapshot != this.data){
this.snapshot.sort(direction, fn, this);
}}});

mystix
29 Oct 2009, 11:40 PM
@nciportal: i'd advise against that and stick with @jburnhams' suggestion instead -- it's simply more flexible + future-proof.