Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re (tilly) 1: Sort on boredom

by tilly (Archbishop)
on Mar 23, 2001 at 18:09 UTC ( [id://66623]=note: print w/replies, xml ) Need Help??


in reply to Sort on boredom

Only inefficiently. If you don't tell Perl to build a hash lookup, you will have to repeatedly scan the array. But you can eliminate the Schwartzian sort:
use strict; use Data::Dumper; $Data::Dumper::Indent = 1; my @vals = qw( 1 2 3 1 3 3 3 43 bob 2 bob 6 2 ); my %freqs; $freqs{$_}++ foreach @vals; my @sorted = sort { $freqs{$b} <=> $freqs{$a} or $a cmp $b } keys %freqs; print Data::Dumper->Dump([\@sorted], ['*sorted']);
Remember that Schwarzian sorts are good when the sort step involves a lot of work. Hash lookups are not that much work. :-)

Incidentally note that way I dump. I had looked that up a long time ago, but I want to start trying to use that for brief one-offs. I think it is rather nice to keep my names intact like that...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://66623]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-09-20 12:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The PerlMonks site front end has:





    Results (26 votes). Check out past polls.

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.