
fishfork
Novice
Aug 9, 2000, 3:39 AM
Post #2 of 2
(253 views)
|
Hi I did something similar myself recently. First of all you need to convert the date into a format that can be sorted numerically. 27/7/00 would need to be encoded as 20000727 In your case ($date,$monthWord,$year) = split(/ /,$thing); if (length($date) == 1) { $data = "0".$date; } so the date is now the required 2 digit number. Now you need the hash %MONTHS = { 'January', '01', ... etc. } You can use this hash to convert $monthWord $monthNum = $MONTHS{$monthWord}; finally $sortable = $year.$monthNum.$date; Now is the sorting. I use a simple bubblesort that is terribly implemented (because I was in a rush and couldn't be bothered. It works, so I haven't fixed it). Here's what I do (which is probably wrong) 1) Get the records from my database and store each in an array recordN where N is a different number for each record. 2) Create an array where entry i is i with 1 entry for each database record. Call this @array 3) This is my bubblesort $exit = 0; while( $exit == 0) { $exit = 1; for($i=0;$i<$#array; $i++) { $pointer = "record".$array[$i]; $pointerr = "record".$array[$i+1]; # suppose the date is in field 3 if ($$pointer[3] gt $$pointerr[3]) { $temp = $array[$i]; $array[$i] = $array[$i+1]; $array[$i+1] = $temp; $exit = 0; }}} this code is off the top of my head, but you get the picture. At the end of it all @array gives the numbers to append to "record" in the sorted order.
|