Discussion Locked

difficult perl sort

0 helpful
9 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I have to set up a perl sort. I need to sort an array of hashs. Basicaly I have an array and in eache element of this array is an anonymous hash. Whithin this hash is a list of key value pairs, three of these pairs have the date in the form of year=>yyyy, month=>mm, day=>dd. What I need to do is to sort this array in order by date.

Sample:

@array = ( {year=>2000,month=>12,day=>19},
{year=>2000,month=>1,day=>20},
{year=>1999,month=>7,day=>8},
{year=>2000,month=>12,day=>26}
{year=>1998,month=12,day=>26} );

I would need this sorted in date order in the array from $array[0] to $array[whatever] so that as new elements are added once i run the sort it would place that element in the appropriate place within the arrray.

500 points for this one.

thanks
Anthony C

9 replies

by lo

Anthony - Been a while since I coded internal sorts but ... There are many ways you can solve this. The 'best' solution depends on various criteria, such as now any entries, performance requirements, available memory, etc. There is a book, I think Kneuth(sp?) as well as others that specify various sort algorithms that will specify which does the best under what conditions.

If you only have a few entries (1000-2000, maybe more) you could just bubble sort. If you have lots (>10000 maybe), one of the most efficinet is a linked list, a separate with key and previous and next entry in sequence. You store the data in an array at the next unused location, binary search the key array and update next/previous pointers to link the new entry in. There is a 'lowest entry' and 'last entry' that locates beginning and ending of the list. Hope that at least points you in the right direction. lo

by basilisk

As the above post stated, you'll really have to look at the conditions under which you will be sorting to figure out the best sorting algorithm. But whatever sort you use, here's how you can test successive elements to see if they need to be swapped. (Sorry, it's in C++, I don't know Perl well.)

if (array[i].year > array[i+1].year)
{ //code to swap array[i] with array[i+1] }
else if (array[i].year == array[i+1].year &&
array[i].month > array[i+1].month)
{ //code to swap array[i] with array[i+1] }
else if (array[i].year == array[i+1].year &&
array[i].month == array[i+1].month
&& array[i].day > array[i+1].day)
{ //code to swap array[i] with array[i+1] }

(This should sort in increasing order.)

by basilisk

I've been thinking: if you are only going to be adding a few new elements each time to an array that is mostly old elements already sorted, and the new elements are most likely to be dated later than the old elements, then a insertion sort (modifiedto start with the new elements instead of at the beginning of the array) would probably be fairly quick and easy to implement.

by royaliel

@sortedarray=sort
{
($a->{year}*10000+$a->{month}*100+$a->{day})
<=>
($b->{year}*10000+$b->{month}*100+$b->{day})
} @array;

Rather obvious IMHO. Not fast though, probably.

This discussion is closed to new replies.