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

Alphabetical Sorts

Status
Not open for further replies.

Siberdude

Programmer
Apr 4, 2000
29
GB
Hi. I have an array containing dates and times.
I want to sort them out, and place them in array in order (so the newest date is in slot0, and the oldest at the other end). I then want to use the date with another string from another array, containing related data and print this out in table form, so no matter where the entry is stored, it will always print out the newest date at the top of the table.
Also, whats the best way to retreive time and date only?
localtime() returns a DAY HH:MM:SS DD/MM/YYYY format
thanks
Siberdude
siberdude@settlers.co.uk
 
($crt_min,$crt_hour,$crt_day,$crt_month,$crt_year)=(localtime)[1..5];



then you can just format it anyway you please :)





to sort the dates, I am sure there is a module that would let you compare two dates, but I really don't know what it would be.





The way I would approach the problem is to just go through a sequence of if statements comparing the years, month, days, hours, minutes. I am sure somebody already made a module that would do all those things for you, but like I said - don't know =)


 
I'm not sure I completely understand your situation, but...... I have found it easy to keep dates in a format like that returned by the 'time' function. "The number of non-leap seconds since Jan. 1, 1970." That way, sorting and comparing dates/times is trivial. They are just integers. You can use the Time::Local module to convert formatted dates to seconds and play with them that way. The following illustrates converting a formatted date to perl epoc seconds and then back to a date with the localtime function.

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

$start_date = "01-01-1985";
@date = split(/-/,$start_date);

$seconds = timelocal(0,0,0,$date[0],$date[1] - 1,$date[2]);
print "seconds => $seconds\n";

$date = localtime($seconds);
print "Date is $date\n";

HTH


keep the rudder amid ship and beware the odd typo
 
If I understand the other part correctly, then you are just trying to sort a bunch of data in chronological order which is stored in two corresponding arrays: dates and data for that date. If I understood that correctly, then you can just do any kind of sort on the dates array, after converting the dates to seconds like goBoating suggested, and at the same time swap the data array to keep nsync;






here is a sample code. I am writing this right now, so if it doesn't work, try to fix it, and if you can't just e-mail me, and i'll check it.







#bubble sort







# @dates - array with dates



# @array - your data







$length=@dates; #length of array



for($i=0; $i<$length; $i++)



{



$swaps=0;



for($j=0; $j<($length-1); $j++)



{



if($dates[j]>$dates[j+1])



{



#swap both array to keep in sync



$tmp=$dates[j];



$dates[j]=$dates[j+1];



$dates[j+1]=$tmp;







$tmp=$array[j];



$array[j]=$array[j+1];



$array[j+1]=$tmp;



}







}







if($swaps==0) {last;}



}











 
goBoating, It would
timelocal(0,0,0,$date[1],$date[0] - 1,$date[2]);

right? since the format is usually MM-DD-YY

 
What ever pieces parts you (or SiberDude) want. My example may have been overly generic. But, depending on the original date format, the split would give the pieces in different orders and you would dereference them from that list as needed.

Oh, yeah, one more detail. I don't know how this treatment will behave if the date precedes the Perl epoc. So, if your dates do precede Jan 1, 1970, be careful.

HTH


keep the rudder amid ship and beware the odd typo
 
If you store dates in &quot;YYYY/MM/DD&quot; format you can sort them (as strings) and they will sort correctly.

If you want to keep TWO arrays in sync while sorting one of them, my advice would be to convert the date array to a hash, with the date as the key and a reference to the corresponding array entry (or simply the array index) as the value. Then you can sort the keys of the hash and access the other array in the proper order without having to rearrange it. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Any way to sort them with out hacing to turn them into seconds first? I'm not the server admin, so i can't install any modules. While this is a big pain in the ass, it forces me to do things the hard way and i learn more.
bassivally i want to be able to chronologically sort an array containing this:

Subject of post~author~date/time of last reply~

i can make this into a hash something like

Code:
open(FILE, &quot;index.dat&quot;) || die &quot;failed to open $!&quot;;

@file = <FILE>;

close(FILE);

for($i = 0; $i <= $#file; $i++)
{
@temp = split (/~/, $file[$i]);
$dates{$temp[2]} = $file[$i];
}
so now i have a hash with the dates as the key, and the string i then want to use in the slot.
How could i then make a loop to sort them into chronological order so i could have the newest one printed first? ie:

Code:
$dates{5-07-01 11:26:21}
$dates{5-07-01 10:22:10}
so on and so forth until all of index.dat has been sorted

can anyone come up with a solution to that? without using modules?
Cheers
Siberdude
siberdude@settlers.co.uk
 
Actually, you don't need to turn them into a hash first. You can just sort your @file array. This should do it:
Code:
@file = sort byDate @file;
And the sort routine:
Code:
sub byDate {
   (undef,undef,$dt_a) = split(/~/,$a);
   (undef,undef,$dt_b) = split(/~/,$b);
   ($mo_a,$dd_a,$yy_a,$hh_a,$mn_a,$ss_a) = 
       ($dt_a =~ /(\d+)-(/d+)-(\d+)\s+(\d+):(\d+):(\d+)/;
   ($mo_b,$dd_b,$yy_b,$hh_b,$mn_b,$ss_b) = 
       ($dt_a =~ /(\d+)-(/d+)-(\d+)\s+(\d+):(\d+):(\d+)/;
   return ($yy_a <=> $yy_b) || 
          ($mo_a <=> $mo_b) || 
          ($dd_a <=> $dd_b) ||
          ($hh_a <=> $hh_b) ||
          ($mn_a <=> $mn_b) ||
          ($ss_a <=> $ss_b);
}
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top