in reply to Re: Is it possible to sort using a coderef, without first storing the coderef in a scalar
in thread Is it possible to sort using a coderef, without first storing the coderef in a scalar
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;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Is it possible to sort using a coderef, without first storing the coderef in a scalar
by ikegami (Patriarch) on Jul 26, 2006 at 05:38 UTC | |
|
Re^3: Is it possible to sort using a coderef, without first storing the coderef in a scalar
by davido (Cardinal) on Jul 26, 2006 at 05:33 UTC |