General discussion

Locked

difficult perl sort

By anthonyc ·
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

This conversation is currently closed to new comments.

9 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

difficult perl sort

by Lo In reply to difficult perl sort

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

Collapse -

difficult perl sort

by anthonyc In reply to difficult perl sort

Poster rated this answer

Collapse -

difficult perl sort

by basilisk In reply to difficult perl sort

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.year > array.year)
{ //code to swap array with array }
else if (array.year == array.year &&
array.month > array.month)
{ //code to swap array with array }
else if (array.year == array.year &&
array.month == array.month
&& array.day > array.day)
{ //code to swap array with array }

(This should sort in increasing order.)

Collapse -

difficult perl sort

by basilisk In reply to difficult perl sort

Yeah, yeah, okay, it's pseudo-C++. TechRepublic doesn't like my formatting.

Collapse -

difficult perl sort

by basilisk In reply to difficult perl sort

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.

Collapse -

difficult perl sort

by anthonyc In reply to difficult perl sort

Poster rated this answer

Collapse -

difficult perl sort

by Royaliel In reply to difficult perl sort

@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.

Collapse -

difficult perl sort

by anthonyc In reply to difficult perl sort

Exactally what I was looking for.

Thank you.

Collapse -

difficult perl sort

by anthonyc In reply to difficult perl sort

This question was closed by the author

Back to Web Development Forum
9 total posts (Page 1 of 1)  

Related Discussions

Related Forums