If your data is already in the format shown below as yearmonthday, then a
standard sort would do just fine:
@test =(20010327, 20001113, 20011225, 19991231, 20100115);
foreach (sort @test)
{
print "$_\n";
}
Output will be:
19991231
20010327
20011225
20100115
On the other hand, you may want a more robust sort to deal with an entire
list of records with multiple fields and sort options. Let's say that your
data is an array of strings with delimeted fields in each string. I used
the pipe symbol "|" as the delimeter since it rarely shows up in the data I
deal with. As you can see each record (array element) has 4 fields. Using
the example below, you can sort each record by a virtually unlimited set of
criteria and return the results into a new array.
@test =
("Lesley|19720702|F|Parent","Chris|19670524|M|Parent","John|20010713|M|Child
","Mitch|19981203|M|Child");
{
@sorted_test = map{$_->[0] }
sort {
@a_fields = @$a[1..$#$a];
@b_fields = @$b[1..$#$b];
$b_fields[3] cmp $a_fields[3]
||
$a_fields[1] <=> $b_fields[1]
}
map { [$_, split /\|/] } @test;
}
foreach (@sorted_test)
{
print "$_\n";
}
The above code will give a primary alphabetical (cmp operator) sort in
descending order ($b_fields before $a_fields) on the fourth field ([3] since
the array is zero based) with a secondary ( || the or symbol )numeric (<=>
operator) sort in ascending ($a_fields before $b_fields)order on the second
field ([1] since tha array is zero based).
Chris Rogers
Vifan USA, Inc.
-----Original Message-----
From: Daniel Falkenberg [mailto:[EMAIL PROTECTED]]
Sent: Monday, August 06, 2001 2:54 AM
To: Beginners-Cgi (E-mail); Beginners (E-mail)
Subject: SORTING BY DATE
List,
Does any one know the best way of sortig by YEAR MONTH DAY?
I would like my script to dispalay 20010327 which is YEAR 2001 MONTH 10 and
DAY 27
Regards,
Daniel Falkenberg
==============================
VINTEK CONSULTING PTY LTD
(ACN 088 825 209)
Email: [EMAIL PROTECTED]
WWW: http://www.vintek.net
Tel: (08) 8523 5035
Fax: (08) 8523 2104
Snail: P.O. Box 312
Gawler SA 5118
==============================
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]