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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sort by date (w/o module)

Status
Not open for further replies.

Alphabin

Programmer
Dec 8, 2002
119
CA
Hi,

I would like to know if there is a simply way to sort by date. Dates are the the following format:
$date = yyyy-mm-dd

Right now I'm doing a simple num sorting (which certainly doesnt work)

Code:
sub sorting {

	$b->[14] <=> $a->[14];
}
 
I've temporarily change the <=> to cmp and it's working...
 
I don't know if you would consider this simple, but it works.

Code:
my (@unsorted, @sorted);
while (<DATA>) {
    chomp;
    push @unsorted, [split(/-/, $_)];
}

@sorted = sort {${$a}[0] <=> ${$b}[0] ||
                ${$a}[1] <=> ${$b}[1] ||
                ${$a}[2] <=> ${$b}[2]} @unsorted;

foreach (@sorted) {
    print "@{$_}\n";
}

__DATA__
2004-01-01
2003-06-19
2004-05-30
2002-09-05

If I remember correctly, over a large number of sorts, cmp is faster than <=>, but there are a number of other factors that are involved as well. The sort you are doing is probably a bit easier to use.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top