1. #1
    Sencha User
    Join Date
    Oct 2007
    Location
    Berlin, Germany
    Posts
    891
    Vote Rating
    9
    wm003 will become famous soon enough

      0  

    Default Array.sortDates ,Array.unique, and extended Array.indexOf

    Array.sortDates ,Array.unique, and extended Array.indexOf


    PHP Code:
    //Sorts an Array that consists of date-objects only (Ext-specific)
    Array.prototype.sortDates = function() {
        return 
    this.sort(function(a,b){
            return 
    parseInt(a.format("YmdHis"))-parseInt(b.format("YmdHis"));
        });
    }; 
    The following was found at http://www.4umi.com/web/javascript/array.htm
    (There are some more useful prototypes ...)

    PHP Code:
    //override the existing indexOf to be able to look after a specific index and check vartypes
    //extended with begin and strict attributes (backward compatible to current ext-indexOf function!)
    Array.prototype.indexOf = function( ob) {
        for( var 
    = +|| 0len this.lengthleni++) {
    //check for objecttype to make this function work with date-objects also
            
    if( this[i]===|| && this[i]==o  || Ext.isDate(o) && this[i].toString()==o.toString()) {
                return 
    i;
            }
        }
        return -
    1;
    };

    // Array.unique( strict ) - Remove duplicate values
    //based on new indexOf defined above
    Array.prototype.unique = function(b) {
        var 
    = [], ilen this.length;
        for(
    i=0i<leni++ ) {
            if( 
    a.indexOfthis[i], 0) < ) {
                
    a.pushthis[i] );
            }
        }
        return 
    a;
    }; 

  2. #2

  3. #3
    Sencha User
    Join Date
    Oct 2007
    Location
    Berlin, Germany
    Posts
    891
    Vote Rating
    9
    wm003 will become famous soon enough

      0  

    Default


    Needed to FIx the indexOf routine again to check for _real_ dateobjects....if you were going to use the above routine, you need to update, otherwise some important ext-features might not work correctly anymore

  4. #4
    Sencha - Community Support Team mystix's Avatar
    Join Date
    Mar 2007
    Location
    Singapore
    Posts
    6,236
    Vote Rating
    5
    mystix will become famous soon enough

      0  

    Default


    to make Date sorting non-Ext specific, you could simply implement sortDates() using the Date object's built-in getTime() method
    Code:
    // Sorts an Array that consists of date-objects only
    Array.prototype.sortDates = function() {
        return this.sort(function(a, b) {
            return a.getTime() - b.getTime();
        });
    };

Thread Participants: 1