A revised version of the set of date sorting functions I've posted, thanks
to the list member who helped me.

var objectArray = [ {d: new Date( 1901, 1, 1)}, {d: new Date()}, {d: new
Date(2003,1,5)}, {d: new Date(2005,3,5)}, {d: new Date(2005,3,5)}, {d: new
Date(2003,1,5)} ];
var dateArray = [ new Date( 1901, 1, 1), new Date(), new Date(2003,1,5), new
Date(2005,3,5), new Date(2005,3,5), new Date(2003,1,5) ];

//wraps Array sort() method using a compare function for Date Objects
function sortDate(a:Array, j:Boolean) {
    a.sort(sortDateCompare, j*2);
}

//compare function for Date Objects, used with the Array sort() method
function sortDateCompare(a,b){
    a=a.valueOf(), b=b.valueOf();
    return a>b?1:a==b?0:-1;
}

//sorts an array of objects, where k points to the date object within each
containing object.
function sortOnDate(a:Array, k:String, j:Boolean) {
    var c=a.length, e=-1, g, i, b, d;
    while(++e<c){
        for(g=-1, i=c-e-1; ++g<c-e;)
             b=a[i][k].valueOf(), d=a[g][k].valueOf(), i=j?b>d?g:i:b<d?g:i;
        a.push(a.splice(i, 1)[0]);
    }
}

//sorting function independent of Array.sort() method and comparison
function
function sortDateArray(a:Array, j:Boolean) {
    var c=a.length, e=-1, g, i, b, d;
    while(++e<c){
        for(g=-1, i=c-e-1; ++g<c-e;)
             b=a[i].valueOf(), d=a[g].valueOf(), i=j?b>d?g:i:b<d?g:i;
        a.push(a.splice(i, 1)[0]);
    }
}

//sortDate(objectArray, 'd', true);
//sortDate(sortDateCompare, true);
//dateArray.sort(sortDateCompare, 2);

//for(var i=-1;++i<objectArray.length;)trace(i+': '+objectArray[i].d);
for(var i=-1;++i<dateArray.length;)trace(i+': '+dateArray[i]);

Hope everyone finds this as useful as I've found other items on this list :)
_______________________________________________
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to