in reply to Stupid, yet simple, sort question

As noted in chatter,

foreach my $loc (sort {$b->{users} <=> $a->{users}} @locations){ ... }

should do the trick.

Update: updated to what joealba was suggesting as I updated it (though without the intermediate step implied by what I said in chatter right before ;-).

Update 2: or, using the correct field...

foreach my $loc (sort {$b->{online} <=> $a->{online}} @locations){ ... }

<sheepish grin>



If God had meant us to fly, he would *never* have given us the railroads.
    --Michael Flanders

Replies are listed 'Best First'.
Re: Re: Stupid, yet simple, sort question
by joealba (Hermit) on Oct 24, 2001 at 20:44 UTC
    Just sort it right the first time.. don't reverse it after.
    foreach my $loc (sort {$b->{users} <=> $a->{users}} @locations) { ... }
    UPDATE: I wanna be like ChemBoy when I grow up. :)

    UPDATE2:Kage, please do yourself a favor. Restructure your data structures into one nice neat hash.
    %pages = ( 'page1' => 2, 'page2' => 24, 'page3' => 8, ); foreach my $loc (sort {$pages{$b} <=> $pages{$a}} keys %pages) { my $s = "s" unless $pages{$loc} == 1; print "$pages{$loc} user$s accessing $loc\n"; ... }

    Update 129: See the entire recoded solution below