Welcome to WebmasterWorld Guest from 54.242.63.214

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

Perl Multidimensional Array Sorting by child

     

JScript5

11:12 pm on Mar 21, 2006 (gmt 0)

5+ Year Member



An old post suggested that the following line would work for my purposes:
@sorted = sort { $b[0] <=> $a[0] } @notsorted;

However, my code actually looks like:
@pics = sort { $a[2] cmp $b[2] } @pics;

where any $pics[$i] = [name, num, yyyy-mm-dd] or [name, num, "false"]
So basically, I'm trying to sort @pics by date, but Perl seems to just skip over this line...

perl_diver

3:49 am on Mar 22, 2006 (gmt 0)

5+ Year Member



you probably just need to add the arrow operator because you have what looks like an array of arrays:

@pics = sort { $a->[2] cmp $b->[2] } @pics;

but the sort will may not work well anyway. using "cmp" to sort yyyy-mm-dd isn't going to return what you expect. Neither is using <=> since I believe the sort will stop at the first dash '-' in the date format. To get an accurate sorting of a date in that format you probably have to convert it back to epoch time and use <=> to sort it.

JScript5

7:40 pm on Mar 23, 2006 (gmt 0)

5+ Year Member



Adding the arrows worked perfectly! I'll need to remember that. Thanks!

perl_diver

9:19 pm on Mar 23, 2006 (gmt 0)

5+ Year Member



also remember you only need the arrows if you are sorting (or doing anything else) a reference to an array.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month