1. #1
    Sencha User
    Join Date
    Apr 2012
    Posts
    12
    Vote Rating
    0
    Answers
    1
    claritee is on a distinguished road

      0  

    Default Answered: Sorting a store when record field with no value

    Answered: Sorting a store when record field with no value


    Hello,

    I have been adding sorters on a store and noticed that if a record does not have a value for a field remains at the top of the sort.

    Just wondering is there a way to order these records to the end of the list?

    Code:
    --- The Store ---
    Ext.define('MyApp.store.PropertySummaries', {
        extend: 'Ext.data.Store',
        xtype: 'PropertySummaries',
    
    
        config: {
            model: 'MyApp.model.PropertySummary',
            sorters: ['propertyName', 'minPrice', 'maxAdults', 'consumerRating']
        }
    });
    ....
    
    --- called by ---
    theStore.sort('consumerRating', 'DESC');
    In this case if there is no consumerRating, I want the record to be at the bottom of the list.

    Thanks in advance!

  2. You could add your own Ext.util.Sorter with a custom sortFn to the store's sorters.

  3. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    St. Louis, MO
    Posts
    33,714
    Vote Rating
    438
    Answers
    3113
    mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of

      0  

    Default


    You could add your own Ext.util.Sorter with a custom sortFn to the store's sorters.
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    http://www.JSONPLint.com - Source to lint your JSONP!

    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 almost in print!

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

  4. #3
    Sencha User
    Join Date
    Apr 2012
    Posts
    12
    Vote Rating
    0
    Answers
    1
    claritee is on a distinguished road

      0  

    Default


    Thanks for the quick reply!

    I tried with the below and that worked

    Code:
    theStore.sort([{
        direction: 'ASC',
        sorterFn: function(o1, o2) {
            return compare(o1.data.consumerRating, o2.data.consumerRating);
        }
    }]);
    
    ....
    
    
    
    theStore.sort([{
        direction: 'DESC',
        sorterFn: function(o1, o2) {
            return compare(o1.data.consumerRating, o2.data.consumerRating);
        }
    }]);
    
    
    .... // etc ....
    
    ....
    // not the most elegant
    function compare(value1, value2) {
        if (!value1 || value1 == '') {
            return -1;
        }
        if (!value2 || value2 == '') {
            return 1;
        }
        if (value1 > value2) {
            return 1;
        }
        if (value1 == value2) {
            return 0;
        }
        return -1;
    }
    I was wondering if there is a way to define multiple sorterFns?
    I tried to define multiple sorter functions but I think there is an error somewhere as the sorterFn isn't getting invoked (or default one is being invoked)

    Code:
    Ext.define('StayzApp.store.PropertySummaries', {
        extend: 'Ext.data.Store',
        xtype: 'PropertySummaries',
    
    
        config: {
            model: 'StayzApp.model.PropertySummary',
            sorters: [
                {property : 'propertyName'},
                {property : 'minPrice'}, // TODO also define custom sorterFn
                {property : 'maxAdults'}, // TODO also define custom sorterFn
                {
                    property : 'consumerRating',
                    sorterFn: function(o1, o2) {
                        return compare(o1.data.consumerRating, o2.data.consumerRating);
                    }
                }
            ]
        }
    });
    
    --- and to invoke ---
    
    theStore.sort('consumerRating', 'ASC'); or
    theStore.sort('consumerRating', 'DESC');
    Thanks for your help