PDA

View Full Version : NodeInterface numerical sort of strings AND numbers



greglafrance007
9 Dec 2014, 11:25 AM
We need to do a numerical sort on nodes that might have numbers or strings.

How to devise a sort function that returns -1 0 1 for a numerical sort in this case?

Thanks in advance !

aaugen
10 Dec 2014, 12:58 AM
Hi greglafrance007,

I'm not sure I understand your problem but if you use a treeStore (and this should be probably the case), you can define on an Ext.util.Sorter (http://docs.sencha.com/extjs/4.2.3/#!/api/Ext.util.Sorter) to sort different fields. This sort can be customized with sorterFn (http://docs.sencha.com/extjs/4.2.3/#!/api/Ext.util.Sorter-cfg-sorterFn) method like in the following example on a standard store (but it must be the same on a treestore):



Ext.create('Ext.data.Store', {
model: 'Person',
proxy: 'memory',
sorters: [{

sorterFn: function(o1, o2){

var getRank = function(o){

var name = o.get('rank');
if (name === 'first') {

return 1;

} else if (name === 'second') {

return 2;

} else {

return 3;

}

},

rank1 = getRank(o1),
rank2 = getRank(o2);

if (rank1 === rank2) {

return 0;

}

return rank1 < rank2 ? -1 : 1;


}

}],
data: [{

name: 'Person1',
rank: 'second'

}, {

name: 'Person2',
rank: 'third'

}, {

name: 'Person3',
rank: 'first'

}]
});

greglafrance007
10 Dec 2014, 10:53 AM
Thanks for the reply! I saw that NodeInterface has a sort() function, and that the second arg of true allows you to sort down the tree hierarchy.

But I am using a TreeStore, so perhaps I should try your idea.

What I was trying to achieve is a numeric sort of string data, so 10 comes after 2.

greglafrance007
10 Dec 2014, 11:16 AM
I tried this, but alas, it did not sort:

store: Ext.create('Ext.data.TreeStore', {
model: 'MyTreeModel',
sorters: [{
property:'text',
direction: 'ASC'
}],
root: {
"expanded": true,
"children": []
}
})

I should say that I am using ExtJS 4.2.2, and have a treepanel. The first column is a treecolumn, and there is another column after that. I want to sort the treecolumn. I assume the second column will follow the tree sort.

evant
10 Dec 2014, 12:48 PM
Specify the sortType on the field: http://docs.sencha.com/extjs/4.2.3/#!/api/Ext.data.Field-cfg-sortType

chamacs
10 Dec 2014, 1:29 PM
Independently of the sortType option mentioned, here is a fiddle with a custom sorter. It may provide some insight to what you need.

https://fiddle.sencha.com/#fiddle/esl

greglafrance007
12 Dec 2014, 3:05 PM
The Fiddle helped me achieve the following, thanks!


this.getMyTree().getStore().sort({
sorterFn: function(a,b){
var test1 = a.get('text');
var test2 = b.get('text');


var ipregex = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;


if(ipregex.test(test1) && ipregex.test(test2)) {
test1 = test1.replace(/\./g, '');
test2 = test2.replace(/\./g, '');
}


var number1 = Ext.isNumeric(test1) ? test1*1 : test1;
var number2 = Ext.isNumeric(test2) ? test2*1 : test2;


if (Ext.isNumber(number1) && Ext.isNumber(number2)) {
// Number compare
if (number1 == number2) {
return 0;
}


return number1 < number2 ? -1 : 1;
}


// String compare
if (test1 == test2) {
return 0;
}


return test1 < test2 ? -1 : 1;


}
});