Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 05-17-2004, 06:58 PM   #1
rob446
EAT, DRINK AND BE MERRY!
 
Join Date: Dec 2001
Location: ENGLAND
Posts: 30
Default Sorting an array of dates

Hi folks,

If anyone can point me in the direction of a way to sort an array of dates, I would be most grateful.

e.g. array containing 19/06/2004,15/05/2004,17/05/2004,08/05/2003 sorted to result in 08/05/2003,15/05/2004,17/05/2004,19/06/2004

Thank you
rob446 is offline   Reply With Quote
Old 05-17-2004, 07:39 PM   #2
pixelwit
village halfwit
 
pixelwit's Avatar
 
Join Date: Jul 2001
Location: USA, PA
Posts: 3,330
Default

PHP Code:
myDates = ["19/06/2004""15/05/2004""17/05/2004""08/05/2003"];
function 
dateSort(d1d2){
    
d1 d1.split("/");
    
d2 d2.split("/");
    if(
d1[2]>d2[2]){
        return 
1
    
}else if(d1[2]<d2[2]){
        return -
1;
    }else{
        if(
d1[1]>d2[1]){
            return 
1;
        }else if(
d1[1]<d2[1]){
            return -
1;
        }else{
            if(
d1[0]>d2[0]){
                return 
1;
            }else if(
d1[0]<d2[0]){
                return -
1;
            }
        }
    }
}
trace(myDates);
myDates.sort(dateSort);
trace(myDates); 
-PiXELWiT
http://www.pixelwit.com
__________________
There are no answers, only choices.
pixelwit is offline   Reply With Quote
Old 05-17-2004, 08:32 PM   #3
ericlin
Registered User
 
Join Date: Jun 2003
Posts: 621
Default

I have another option. However, this is less efficient than the code pixelwit post.

If the date format is always in the form of dd/mm/yyyy, then we can re-arrange them as yyyy/mm/dd. Then do the comparison.
ericlin is offline   Reply With Quote
Old 05-18-2004, 03:40 AM   #4
rob446
EAT, DRINK AND BE MERRY!
 
Join Date: Dec 2001
Location: ENGLAND
Posts: 30
Default

Thank you very much PiXELWiT. I was thinking along those lines myself , but couldn't quite get my head around the function for array.sort.
rob446 is offline   Reply With Quote
Old 05-18-2004, 04:07 AM   #5
ericlin
Registered User
 
Join Date: Jun 2003
Posts: 621
Default

PHP Code:
myDates = ["19/06/2004""15/05/2004""17/05/2004""08/05/2003"];
function 
dateSort(d1d2) {
    
d1 d1.split("/").reverse().join();
    
d2 d2.split("/").reverse().join();
    return (
d1>d2) ? : -1;
}
trace(myDates);
myDates.sort(dateSort);
trace(myDates); 
ericlin is offline   Reply With Quote
Old 05-18-2004, 04:29 PM   #6
pixelwit
village halfwit
 
pixelwit's Avatar
 
Join Date: Jul 2001
Location: USA, PA
Posts: 3,330
Default

You're welcome Rob446.

That's pretty sweet Ericlin, I like it. I thought your way might run a little quicker than mine because all my comparisons use array notation but it turns out that you're right again. My way takes about 80% of the time it takes to run your code. Thanks for your alternative point of view.

-PiXELWiT
http://www.pixelwit.com
__________________
There are no answers, only choices.
pixelwit is offline   Reply With Quote
Old 05-18-2004, 07:08 PM   #7
ericlin
Registered User
 
Join Date: Jun 2003
Posts: 621
Default

Yea, I think Array.reverse and Array.join are slow.

BTW, it seems that you could remove all the "else" in your code.
ericlin is offline   Reply With Quote
Old 05-18-2004, 07:35 PM   #8
pixelwit
village halfwit
 
pixelwit's Avatar
 
Join Date: Jul 2001
Location: USA, PA
Posts: 3,330
Default

True, the code could be written as:
PHP Code:
function dateSort2(d1d2) {
    
d1 d1.split("/");
    
d2 d2.split("/");
    if (
d1[2]>d2[2]) return 1;
    if (
d1[2]<d2[2]) return -1;
    if (
d1[1]>d2[1]) return 1;
    if (
d1[1]<d2[1]) return -1;
    if (
d1[0]>d2[0]) return 1;
    if (
d1[0]<d2[0]) return -1;

But I like the seeing the structure of the code in the first example plus it's a little easier to insert a line if you want to make modifications later. Mostly I do it just because it's a habit I guess, but from what I can tell neither one runs any faster than the other.

Thanks for keeping me on my toes.

-PiXELWiT
http://www.pixelwit.com
__________________
There are no answers, only choices.
pixelwit is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


All times are GMT. The time now is 06:41 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.