I know that the first example would work if I used numeric instead of \&numeric - but why does \&numeric not also work?# Doesn't work sort \&numeric @list; # Works my $sorter = \&numeric; sort $sorter @list;
I'm playing with a module that defines a set of common sort patterns, and provides methods that return coderefs that implement the pattern that was requested.
For example, sorting a list of hashes based on the value of a user specified key. The custom implementation would be something like:
My module provides this:sub sort_hashkey($$) { my ($a,$b) = @_; $a->{foo} <=> $b->{foo}; } my @sorted = sort_hashkey @list;
It would be nice, but not neccesary, to do this:my $sorter = sorthash_numeric('foo'); my @sorted = sort $sorter @list;
my @sorted = sort sorthash_alpha('foo') @list;
In reply to Re^2: Is it possible to sort using a coderef, without first storing the coderef in a scalar
by imp
in thread Is it possible to sort using a coderef, without first storing the coderef in a scalar
by imp
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |