Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sort UK style dates 1

Status
Not open for further replies.

serpento

Programmer
Jun 16, 2002
92
GB
I've got an array full of dates in the form ('dd/mm/yy', 'dd/mm/yy', ... etc. (I'm from the UK). I want to use Perl's built in sort function to sort them in order and in reverse order.

Any help much appreciated.

-Ed ;-)

________________________________
Destiny is not a matter of chance; it is a matter of choice.
 
Hi,

I'm sure there are better ways (Date::Calc ??) however you could convert your date into a timestamp then sort that way...

e.g.

use POSIX qw(mktime);

foreach $x (0..$#date) # assume dates held in @date array
{
($day,$mon,$year)=split (/\//,$date[$x]);
$tstamp = mktime (0,0,0,$day,$mon,$year); # $year=yy
push (@store_time,$tstamp);
}

@ascending = sort @store_time {$a <=> $b};
@descending = reverse sort @store_time {$a <=> $b};

PS ... haven't tried the above code but simple manipulation should be able to get the result you wish

Hope this helps ...
 
... of course the sort syntax should be ...

@ascending = sort {$a <=> $b} @store_time;
@descending = reverse sort {$a <=> $b} @store_time;
 
just re-read your original post ... OK ... I've went off at a tangent ....

... For formatting purposes you would of course also need to convert the dates back to dd/mm/yy, which again is pretty straightforward.

Very Messy !! I'm sure there are far more concise ways of doing this...
 
Thanks for your help, parkers! I didn't know you could just put reverse to sort in reverse.

I think in the end I'll use this code:
Code:
foreach $dream (@tableOfDreams)
{
  @dateOfDream = split('/', uptoFirst("\n", $dream)); #split into dd, mm, yy
  $dream = "$dateOfDream[2]$dateOfDream[1]$dateOfDream[0]$dream"; #reverse the order so its yy, mm, dd
}

sort (@tableOfDreams); #now sort will work

foreach $dream (@tableOfDreams) {$dream = substr($dream, 6);} #delete first six characters so is back to original
Unless anyone has a more efficient idea . . . ?

-Ed ;-)

________________________________
Destiny is not a matter of chance; it is a matter of choice.
 
Again, there's probably something in Date::Calc to suit. Be careful to change your two-digit date to a four-digit one for that to work properly.

The following is an 'old-fashion' method of date comparison. The dates are turned into integers, in the form yyyymmdd (the intify() sub), which can then be compared numerically. I've assumed that any two-digit years from 51 to 99 are 1951 to 1999 and 00 to 49 are 2000 to 2049 (you can change that to suit). To reverse the search order, just switch $a and $b as usual.
Code:
my @dates = qw!25/04/99 01/12/98 04/04/04 29/03/02!;

my @sorted = sort {intify($a) <=> intify($b)} @dates;

sub intify {
    my @arr = split('/', shift);
    return ($arr[2] > 50?'19':'20')."$arr[2]$arr[1]$arr[0]";
}

print "@sorted\n";
 
Cheers ishnid, that looks like a very reasonable way to complete the task and I will definately use it.

Thankyou, have a star.

-Ed ;-)

________________________________
Destiny is not a matter of chance; it is a matter of choice.
 
blimey ishnid - have you been taking steroids!?

another excellent solution...


Kind Regards
Duncan
 
Actually, a friend of mine showed me that once. Apparently it's how they used to compare dates in C:
Code:
int year = 1998;
int month = 12;
int day = 31;

// might need a long - I can't remember
int compare_int = year * 10000 + month * 100 + day;
Of course, in Perl, it's easier just to concatenate strings and let the interpreter turn it into an integer for sorting.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top