1. #1
    Sencha User
    Join Date
    Jan 2013
    Posts
    8
    Vote Rating
    0
    Lolboy is on a distinguished road

      0  

    Default Sorting issue

    Sorting issue


    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:

    Code:
    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:

    Code:
    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:
    Code:
    /**
         * 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:
    Code:
    if (comparison >= 0) {
    Code:
        start = middle + 1;
    

  2. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,004
    Vote Rating
    847
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    To do it properly, IMO you should have 2 sorters, 1 for each field, otherwise you cannot rely on the order.
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is in print!

    When posting code, please use BBCode's CODE tags.

Thread Participants: 1

Tags for this Thread