Unimatic1140 has asked for the wisdom of the Perl Monks concerning the following question:

Hi everyone, I've been working on this all day and have yet figured out the right syntax. I'm selecting data from a database and placing it into a hash. I want to print the post numbers sometimes in ascending order and other times descending order. I got the ascending part down fine, but for the life of me I cannot figure out how to sort it descending. The code to get the data into a hash and sort it ascending is this:
##SELECT DATA FROM DATABASE AND FILL IT INTO A HASH $sth = $dbh->prepare("SELECT * FROM $table_POSTS WHERE Thread = '$Sent +Thread'"); $sth->execute(); $sth->bind_columns(\$THREADd, \$POSTd, \$FORUMd, \$USERNAMEd, \$CREATE +Dd, \$REPLIEDd, \$TITLEd,\$DRIFTd, \$LINKd, \$JPEGd, \$COMMENTSd, \$I +Pd, \$REMd, \$OFFENSIVEd ); while($sth->fetch()) { $view{$POSTd} = { 'thread' => $THREADd, 'post' => $POSTd, 'forum' => $FORUMd, 'username' => $USERNAMEd, 'created' => $CREATEDd, 'replied' => $REPLIEDd, 'title' => $TITLEd, 'drift' => $DRIFTd, 'link' => $LINKd, 'jpeg' => $JPEGd, 'comments' => $COMMENTSd, 'ip' => $IPd, 'rem' => $REMd, 'offensive' => $OFFENSIVEd }; ###PRINT THE POST NUMBERS ASCENDING foreach $view2 ( sort keys %view ) { $POST = $view{$view2}->{'post'}; print "$POST<br>"; }
Can anyone show me how to sort this descending? Big thanks in advance.

Replies are listed 'Best First'.
Re: Shouldn't this sort be simple?
by almut (Canon) on Mar 03, 2010 at 22:37 UTC
    foreach $view2 ( sort {$b cmp $a} keys %view ) {

    sort without an explicit sort routine is the same as sort {$a cmp $b} .... Swapping $a and $b reverses the outcome of the individual pairwise comparisons, and thus also the ordering of the result.

      YAY, thank you almut, that worked perfectly, I was searching the internet all day long and could find the answer. I knew it had to be simple. Thanks again!
Re: Shouldn't this sort be simple?
by ww (Archbishop) on Mar 03, 2010 at 22:49 UTC
Re: Shouldn't this sort be simple?
by jrsimmon (Hermit) on Mar 03, 2010 at 22:39 UTC
    Not elegant, but you get the idea...
    ##SELECT DATA FROM DATABASE AND FILL IT INTO A HASH $sth = $dbh->prepare("SELECT * FROM $table_POSTS WHERE Thread = '$Sent +Thread'"); $sth->execute(); $sth->bind_columns(\$THREADd, \$POSTd, \$FORUMd, \$USERNAMEd, \$CREATE +Dd, \$REPLIEDd, \$TITLEd,\$DRIFTd, \$LINKd, \$JPEGd, \$COMMENTSd, \$I +Pd, \$REMd, \$OFFENSIVEd ); while($sth->fetch()) { $view{$POSTd} = { 'thread' => $THREADd, 'post' => $POSTd, 'forum' => $FORUMd, 'username' => $USERNAMEd, 'created' => $CREATEDd, 'replied' => $REPLIEDd, 'title' => $TITLEd, 'drift' => $DRIFTd, 'link' => $LINKd, 'jpeg' => $JPEGd, 'comments' => $COMMENTSd, 'ip' => $IPd, 'rem' => $REMd, 'offensive' => $OFFENSIVEd }; my @array1; my @array2; ###PRINT THE POST NUMBERS ASCENDING foreach $view2 ( sort keys %view ) { #$POST = $view{$view2}->{'post'}; push @array1, $view{$view2}->{'post'}; unshift @array2, $view{$view2}->{'post'}; #print "$POST<br>"; } foreach my $value (@array1){ print "$value<br>"; } foreach my $value (@array2){ print "$value<br>"; }