NAME

Sort::Key - Perl extension for sorting objects by some key

SYNOPSIS

use Sort::Key qw(keysort nkeysort ikeysort);

@by_name = keysort { "$_->{surname} $_->{name}" } @people;
@by_age = nkeysort { $_->{age} } @people;
@by_sons = ikeysort { $_->{sons} } @people;

DESCRIPTION

Sort::Key provides a set of functions to sort object arrays by some (calculated) key value.

Usually, it is faster and uses less memory than other alternatives implemented around perl sort function.

EXPORT

None by default.

Functions available from this module are:

keysort { CALC_KEY } @array

returns the elements on @array sorted by the key calculated applying { CALC_KEY } to them.

Inside { CALC_KEY }, the object is available as $_.

For example:

@a=({name=>john, surname=>smith}, {name=>paul, surname=>belvedere});
@by_name=keysort {$_->{name}} @a;

This function honours the use locale pragma.

nkeysort { CALC_KEY } @array

similar to keysort but compares the keys numerically instead of as strings.

This function honours the use integer pragma, i.e.:

use integer;
my @s=(2.4, 2.0, 1.6, 1.2, 0.8);
my @ns = nkeysort { $_ } @s;
print "@ns\n"

prints

0.8 1.6 1.2 2.4 2

SEE ALSO

perl sort function, integer, locale.

AUTHOR

Salvador Fandiño, <sfandino@yahoo.com>

COPYRIGHT AND LICENSE

Copyright (C) 2005 by Salvador Fandiño

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.4 or, at your option, any later version of Perl 5 you may have available.