1. #1
    Sencha User
    Join Date
    Mar 2007
    Location
    Boulder, CO
    Posts
    69
    Vote Rating
    0
    ericwaldheim is on a distinguished road

      0  

    Default grid stable sort

    grid stable sort


    drop-in replacement for DefaultDataModel sort:

    Code:
    YAHOO.ext.grid.DefaultDataModel.prototype.sort = function(sortInfo, columnIndex, direction, suppressEvent){
       // store these so we can maintain sorting when we load new data
        this.sortInfo = sortInfo;
        this.sortColumn = columnIndex;
        this.sortDir = direction;
       
        var dsc = (direction && direction.toUpperCase() == 'DESC');
        var sortType = null;
        if(sortInfo != null){
            if(typeof sortInfo == 'function'){
                sortType = sortInfo;
            }else if(typeof sortInfo == 'object'){
                sortType = sortInfo.getSortType(columnIndex);;
            }
        }
        var data = this.data;
        var ds = [];
        for (var i = 0; i < data.length; ++i)
            ds.push([data[i][columnIndex], i]);
    
        var fn = function(d1, d2){
            var v1 = sortType ? sortType(d1[0], d1) : d1[0];
            var v2 = sortType ? sortType(d2[0], d2) : d2[0];
            if (v1 < v2) return dsc ? +1 : -1;
            if (v1 > v2) return dsc ? -1 : +1;
            if (d1[1] < d2[1]) return dsc ? +1 : -1;
            return dsc ? -1 : +1;
        };
        ds.sort(fn);
        var newData = [];
        for (var i = 0; i < ds.length; ++i)
            newData[i] = data[ds[i][1]];
        this.data = newData;
    
        if (!suppressEvent){
            this.fireRowsSorted(columnIndex, direction);
        }
    }

  2. #2
    Sencha User jack.slocum's Avatar
    Join Date
    Mar 2007
    Location
    Tampa, FL
    Posts
    6,955
    Vote Rating
    17
    jack.slocum will become famous soon enough jack.slocum will become famous soon enough

      0  

    Default


    What does it do differently?

  3. #3
    Sencha User jack.slocum's Avatar
    Join Date
    Mar 2007
    Location
    Tampa, FL
    Posts
    6,955
    Vote Rating
    17
    jack.slocum will become famous soon enough jack.slocum will become famous soon enough

      0  

    Default


    Ah I just saw the title. Cool.

  4. #4
    Sencha User
    Join Date
    Mar 2007
    Location
    Boulder, CO
    Posts
    69
    Vote Rating
    0
    ericwaldheim is on a distinguished road

      0  

    Default


    This one is more efficient:
    Code:
    YAHOO.ext.grid.DefaultDataModel.prototype.sort = function(sortInfo, columnIndex, direction, suppressEvent){
       // store these so we can maintain sorting when we load new data
       this.sortInfo = sortInfo;
       this.sortColumn = columnIndex;
       this.sortDir = direction;
       
       var dsc = (direction && direction.toUpperCase() == 'DESC');
       var sortType = null;
       if(sortInfo != null){
          if(typeof sortInfo == 'function'){
             sortType = sortInfo;
          }else if(typeof sortInfo == 'object'){
             sortType = sortInfo.getSortType(columnIndex);;
          }
       }
       var data = this.data;
       var ds = [];
       for (var i = 0; i < data.length; ++i)
       {
          var row = data[i];
          var value = row[columnIndex];
          value = sortType ? sortType(value, row) : value;
          ds.push([value, i]);
       }
    
       var fn = function(d1, d2){
          if (d1[0] < d2[0]) return dsc ? +1 : -1;
          if (d1[0] > d2[0]) return dsc ? -1 : +1;
          if (d1[1] < d2[1]) return dsc ? +1 : -1;
          return dsc ? -1 : +1;
       };
       ds.sort(fn);
       var newData = [];
       for (var i = 0; i < ds.length; ++i)
          newData[i] = data[ds[i][1]];
       this.data = newData;
    
       if (!suppressEvent) {
          this.fireRowsSorted(columnIndex, direction);
       }
    }

  5. #5
    Ext User
    Join Date
    Mar 2007
    Posts
    22
    Vote Rating
    0
    glassy is on a distinguished road

      0  

    Default


    Hmm... here's my version of this. I'm wondering which would be more efficient, or if mine is bad practice. It's just what came to my mind first. The changes from the original code are under the // comments

    Code:
    YAHOO.ext.grid.DefaultDataModel.prototype.sort = function(sortInfo, columnIndex, direction, suppressEvent){
        this.sortInfo = sortInfo;
        this.sortColumn = columnIndex;
        this.sortDir = direction;
    
        // we are going to preserve indexes, so save them first    
        for (i = 0; i < this.data.length; i++) {
            this.data[i].index = i;
        }
        
        var dsc = (direction && direction.toUpperCase() == 'DESC');
        var sortType = null;
        if(sortInfo != null){
            if(typeof sortInfo == 'function'){
                sortType = sortInfo;
            }else if(typeof sortInfo == 'object'){
                sortType = sortInfo.getSortType(columnIndex);;
            }
        }
        var fn = function(cells, cells2){
            var v1 = sortType ? sortType(cells[columnIndex], cells) : cells[columnIndex];
            var v2 = sortType ? sortType(cells2[columnIndex], cells2) : cells2[columnIndex];
            if(v1 < v2)
    			return dsc ? +1 : -1;
            if(v1 > v2)
    			return dsc ? -1 : +1;
            // alright, their values are the same, so we use the index to compare
            return cells.index - cells2.index;
        };
        this.data.sort(fn);
        if(!suppressEvent){
           this.fireRowsSorted(columnIndex, direction);
        }
    };

  6. #6
    Ext User
    Join Date
    Mar 2007
    Posts
    22
    Vote Rating
    0
    glassy is on a distinguished road

      0  

    Default


    Hmm.. the most efficient is probably caching the sortType? check like your last one, but maybe adding it to the data like I did so there is no need for the data assigning loop (if that's possible ... )

Similar Threads

  1. Grid: Post column name (not just id) in remote sort
    By brondsem in forum Community Discussion
    Replies: 2
    Last Post: 18 Feb 2007, 4:01 PM
  2. Grid: smart remote vs local sort
    By brondsem in forum Community Discussion
    Replies: 0
    Last Post: 26 Jan 2007, 7:19 AM
  3. grid sort marks all rows by the first column
    By lsmith in forum Ext 1.x: Help & Discussion
    Replies: 8
    Last Post: 22 Nov 2006, 9:13 AM
  4. Grid Sort for formatted date column in FF vs IE6
    By mrim in forum Ext 1.x: Help & Discussion
    Replies: 5
    Last Post: 31 Oct 2006, 8:28 PM
  5. Sort direction and sort images
    By dfenwick in forum Ext 1.x: Bugs
    Replies: 3
    Last Post: 20 Sep 2006, 11:53 PM

Thread Participants: 2