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

How can I sort an array which contains data of "Dates"

Status
Not open for further replies.

sut

Programmer
Jun 6, 2001
12
0
0
US
Hi,
I need to sort an array of values, the values are actually the dates like 01/11/2001, 01/01/2000 etc. If I use sort function it doesn't sort it correctly obvisously because it doesn't compare "date" values it compares "Ascii" value.
Does anybody has an idea how to sort this type of array?

Thanks
S
 
You need to do a multi-level sort. The following will sort in ascending order.
Code:
sub mysort() {
    @data1 = split ("/",$a);
    @data2 = split ("/",$b);
    $data1[2] <=> $data2[2] || $data1[0] <=> $data2[0] || $data1[1] <=> $data2[1];
}
@sortedlist = sort mysort @list;
For descending order, reverse the order of $data1 and $data2.
 
Another approach, TMTOWTDI ;-)

I have taken to storing all my times in seconds since
jan 1, 1970. This makes sorting and comparing and adding and subtracting them simple.

Code:
#!/usr/local/bin/perl
use Time::Local;

@dates = ('01/11/2001',
          '02/01/2002',
          '11/15/1999',
          '12/30/2000',
          '01/25/2001',
          );
# convert dates to seconds.
foreach (@dates)
  {
  my @date = split(/\//,$_);
  $seconds = timelocal(0,0,0,$date[1],$date[0]-1,$date[2]);
  push @dates_by_seconds, $seconds; 
  }

@sorted = sort numerically (@dates_by_seconds);
foreach (@sorted)
  {
  my $date = localtime($_);
  print &quot;DATE: $date\n&quot;;
  }

sub numerically { $a <=> $b }

HTH If you are new to Tek-Tips, please use descriptive titles, check the FAQs,
and beware the evil typo.
 
Thanks raider2001 for your help.
What I want to do is something like this.

I am storing data in 3 hashes depending on the result type.
the (key,value) pair stores &quot;key&quot; and &quot;date & Result type on that date&quot;.
I can get the values from the hash, store it in an array and sort it (3 arrays).
Now what i want to do is to combine these 3 arrays and sort it. But I also want to store the Result Type like 1,2,3 etc. i.e. I want to use multidimensional array whose every element contains Date and Result Type. I want to sort this multidimensional array. How can I store information in multidimenstional array and sort it.

Waiting for the reply.
Thanks
S
 
Not quite sure what you want. Can you give an example of the data and how you want it sorted? In particular:

What do the 3 hashes look like?
What do you want the combined array to look like?
 
I store monthly HR, IT & General announcements in 3 DBM files.
I retrieve data into 3 hashes,
(key, value) pair stores (id,(date&announcement))

I want only latest 10 announcements irrespective of the announcement type i.e.HR, IT etc.

So what I do, I retrieve the dates into 3 arrays. So I combine these arrays into one array, sort it on Date.
but I also want to store announcements along with the arrays and display announcements along with the date.

Ex. HR Hash
Key Value
1 01/10/2001 401k plan
2 03/04/2000 Holiday list

IT Announcement
Key Value
1 01/11/2001 Windows XP launch
2 03/10/2000 Interwoven

General Announcement
Key Value
1 10/31/2001 Hallowen Party

I can store dates into arrays and sort it but I want the respective announcements.
I can't use &quot;Date&quot; as a key to store these values into new hash because there might be more than one announcements on a particular date.
How can I do it by using multidimensional array or in other way?

Thanks
S

How can I do it?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top