Welcome to WebmasterWorld Guest from 54.196.244.186

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

Perl Multidimensional Array Sorting by child

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

New User

5+ Year Member

joined:Mar 21, 2006
posts:2
votes: 0


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

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

Preferred Member

10+ Year Member

joined:Jan 5, 2006
posts:536
votes: 0


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.

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

New User

5+ Year Member

joined:Mar 21, 2006
posts:2
votes: 0


Adding the arrows worked perfectly! I'll need to remember that. Thanks!
9:19 pm on Mar 23, 2006 (gmt 0)

Preferred Member

10+ Year Member

joined:Jan 5, 2006
posts:536
votes: 0


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