homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
Forum Library, Charter, Moderators: coopster & jatar k & phranque

Perl Server Side CGI Scripting Forum

Perl Multidimensional Array Sorting by child

 11:12 pm on Mar 21, 2006 (gmt 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)

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)

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


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

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

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved