Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Condensed rank table output

by davido (Cardinal)
on Sep 07, 2019 at 21:34 UTC ( [id://11105807]=note: print w/replies, xml ) Need Help??


in reply to Condensed rank table output

print "$_ => $hash{$_}\n" for sort {$hash{$b} <=> $hash{$a} || $a cmp +$b} keys %hash;

Sort in reverse numerical order, and in cases of collisions, in ASCIIbetical order for names. If you wish to be nice and use proper Unicode collation for peoples' names, use Unicode::Collate for the part of the sort routine that comes after the || operator.

use strict; use warnings; use Unicode::Collate; use utf8; binmode STDOUT, ':encoding(UTF-8)'; my $collator = Unicode::Collate->new(); my %hash = ( 'James' => 43, 'John' => 2, 'Jeffrey' => 18, 'Jamie' => 19, 'Joseph' => 43, '&#308;an' => 43, # Was latin capital letter j with circumflex be +fore PerlMonks got a hold of it 'Janice' => 12, 'Jasmin' => 7, 'Justin' => 31, 'José' => 43, 'Jordan' => 43, ); print "$_ => $hash{$_}\n" for sort {$hash{$b} <=> $hash{$a} || $collator->cmp($a,$b)} keys % +hash;

(Sorry about PerlMonks breaking latin capital letter j with circumflex)


Dave

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-03-28 19:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found