Subject: | hashsort |
Date: | Fri, 7 Nov 2008 05:40:23 -0800 |
To: | bug-Hash-MoreUtils [...] rt.cpan.org |
From: | "m c" <rossiya2 [...] gmail.com> |
Hello
I made hashsort work better with prototypes
sub hashsortt(&\%) {
my ($code, $hash) = @_;
unless ($hash) {
$hash = $code;
$code = sub { $a cmp $b };
}
return map {
($_ => $hash->{$_})
} sort {
$code->()
} keys %$hash;
}
x hashsortt { $a cmp $b } %hash;
0 'af'
1 88
2 'f'
3 1
4 'kf'
5 3
6 'zf'
7 5
%rHash = reverse %hash
x hashsortt { $a <=> $b } %rHash;
0 1
1 'f'
2 3
3 'kf'
4 5
5 'zf'
6 88
7 'af'
Probably still does a copy. I used the code in
List::MoreUtils::pairwise for scaffolding