Results 1 to 3 of 3

Thread: Ext.util.MixedCollection.sortByKey does not work

    Success! Looks like we've fixed this one. According to our records the fix was applied for EXTJS-9151 in a recent build.
  1. #1
    Ext JS Premium Member
    Join Date
    Apr 2008
    Posts
    29

    Default Ext.util.MixedCollection.sortByKey does not work

    The following code does not work in 4.2 but in 4.1 the last alert shows "Value A".

    Code:
    var col = new Ext.util.MixedCollection;
    col.add("B", "Value B");
    col.add("A", "Value A");
    alert(col.get(0)); // "Value B"
    col.sortByKey();
    alert(col.get(0)); // Should be "Value A", but is "Value B"

  2. #2
    Sencha User Animal's Avatar
    Join Date
    Mar 2007
    Location
    Bédoin/Nottingham
    Posts
    30,890

    Default

    Thanks for that. Try this override:

    Code:
    Ext.override(Ext.util.MixedCollection, {
        _sort : function(property, dir, fn) {
            var me = this,
                i, len,
                dsc   = String(dir).toUpperCase() == 'DESC' ? -1 : 1,
    
                //this is a temporary array used to apply the sorting function
                c     = [],
                keys  = me.keys,
                items = me.items,
                o;
    
            //default to a simple sorter function if one is not provided
            fn = fn || function(a, b) {
                return a - b;
            };
    
            //copy all the items into a temporary array, which we will sort
            for (i = 0, len = items.length; i < len; i++) {
                c[c.length] = {
                    key  : keys[i],
                    value: items[i],
                    index: i
                };
            }
    
            //sort the temporary array
            Ext.Array.sort(c, function(a, b) {
                return fn(a[property], b[property]) * dsc ||
                    // In case of equality, ensure stable sort by comparing collection index
                    (a.index < b.index ? -1 : 1);
            });
    
            // Copy the temporary array back into the main this.items and this.keys objects
            // Repopulate the indexMap hash if configured to do so.
            for (i = 0, len = c.length; i < len; i++) {
                o = c[i];
                items[i] = o.value;
                keys[i]  = o.key;
                me.indexMap[o.key] = i;
            }
            me.generation++;
            me.indexGeneration = me.generation;
            me.fireEvent('sort', me);
        }
    });

  3. #3
    Ext JS Premium Member
    Join Date
    Apr 2008
    Posts
    29

    Default

    Thanks, that works!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •