[CLOSED] Sort store by multiple fields

  1. #1

    [CLOSED] Sort store by multiple fields

    Hello,

    I was using the below override code to sort a store (locally) by multiple fields however it throws an error when I try with v2. How can I sort the store by multiple fields with the latest version?

    Thanks.

        Ext.override(Ext.data.Store, {
            /**
            * Sort by multiple fields in the specified order.
            * @param {Array} An Array of field sort specifications, or, if ascending
            * sort is required on all columns, an Array of field names. A field specification
            * looks like:<pre><code>
            {
            field: 'orderNumber',
            direction: 'ASC'
            }
            */
            sortByFields: function (fields) {
                //      Collect sort type functions,
                //      Convert string field names to field+direction spec objects.
                var st = [];
                for (var i = 0; i < fields.length; i++) {
                    if (typeof fields[i] == 'string') {
                        fields[i] = {
                            field: fields[i],
                            direction: 'ASC'
                        };
                    }
                    st.push(this.fields.get(fields[i].field).sortType);
                }
    
                var fn = function (r1, r2) {
                    var result;
                    for (var i = 0; !result && i < fields.length; i++) {
                        var v1 = st[i](r1.data[fields[i].field]);
                        var v2 = st[i](r2.data[fields[i].field]);
                        result = (v1 > v2) ? 1 : ((v1 < v2) ? -1 : 0);
                        if (fields[i].direction == 'DESC') result = -result;
                    }
                    return result;
                };
                this.data.sort('ASC', fn);
                if (this.snapshot && this.snapshot != this.data) {
                    this.snapshot.sort('ASC', fn);
                }
                this.fireEvent("datachanged", this);
            }
        });
    Last edited by Daniil; Aug 21, 2013 at 9:07 AM. Reason: [CLOSED]
  2. #2
    Hi @bayoglu,

    There is built-in support of multi-sorting in ExtJS 4. Please clarify did you try it?
    http://docs.sencha.com/extjs/4.2.1/#...le-method-sort
  3. #3
    Hello @Daniil,

    You are right, the built-in function is fine. Please mark as closed.

Similar Threads

  1. [CLOSED] Store: Sort by Multiple Column
    By nhg_itd in forum 1.x Legacy Premium Help
    Replies: 7
    Last Post: Dec 27, 2013, 4:05 AM
  2. [CLOSED] IDProperty: set multiple key fields
    By tanky65 in forum 2.x Legacy Premium Help
    Replies: 2
    Last Post: Jan 23, 2013, 12:03 PM
  3. [CLOSED] Adding multiple Fields
    By sisa in forum 1.x Legacy Premium Help
    Replies: 3
    Last Post: Sep 14, 2012, 1:36 PM
  4. How to sorted by multiple fields?
    By bruce in forum 1.x Help
    Replies: 0
    Last Post: May 04, 2009, 4:10 AM
  5. ComboBox to Sort Store By Fields
    By Tbaseflug in forum 1.x Help
    Replies: 2
    Last Post: Apr 22, 2009, 4:02 PM

Posting Permissions