PDA

View Full Version : Sorting issue



Lolboy
14 Feb 2013, 11:45 PM
Hi guys!

I use sencha touch 2.1 for a month and I got a problem:

Just see the example:
I have a store with a few fields and using standard sorter or sorterFn to sort my store by field1, then I see a strange sorting behavior when I'm changing value of field1:



Ext.application({

launch: function() {


Ext.define("testModel", {
extend: "Ext.data.Model",
config: {
fields: [
{name: "field1", type: "int"},
{name: "field2", type: "int"},
]
}
});


var testStore = Ext.create("Ext.data.Store", {
model: "testModel",
data : [
{field1: 1, field2: 1},
{field1: 1, field2: 2},
{field1: 1, field2: 3},
{field1: 1, field2: 4},
{field1: 1, field2: 5},
{field1: 1, field2: 6}
],
sorters: [
{
property : "field1",
direction: "ASC"
}
]
});


var list = Ext.create("Ext.List", {
fullscreen: true,
store: testStore,
itemTpl: "field1:{field1} field2:{field2}"
});


Ext.Viewport.add(list);

// just change from 1 to 1
testStore.first().set("field1", 1);
}
});




Result is:


field1:1 field2:2
field1:1 field2:3
field1:1 field2:4
field1:1 field2:5
field1:1 field2:6
field1:1 field2:1

But every row has an equal value in field1.
Why does it dropped down?

After editing any of records in the store, store call store.afterEdit method, that calls data.replace(record), that computes index: index = this.findInsertionIndex(items, item);

So, method findInsertionIndex computes new index based on sorters for this store:


/**
* This method returns the index that a given item would be inserted into a given array based
* on the current sorters.
* @param {Array} items The array that you want to insert the item into.
* @param {Mixed} item The item that you want to insert into the items array.
* @return {Number} The index for the given item in the given array based on the current sorters.
*/
findInsertionIndex: function(items, item, sortFn) {
var start = 0,
end = items.length - 1,
sorterFn = sortFn || this.getSortFn(),
middle,
comparison;


while (start <= end) {
middle = (start + end) >> 1;
comparison = sorterFn(item, items[middle]);
if (comparison >= 0) {
start = middle + 1;
} else if (comparison < 0) {
end = middle - 1;
}
}


return start;
}
});

Seems to me, the record in store just cannot stay without moving during the sorting and it drops down because of:


if (comparison >= 0) {
start = middle + 1;

mitchellsimoens
16 Feb 2013, 2:15 PM
To do it properly, IMO you should have 2 sorters, 1 for each field, otherwise you cannot rely on the order.