The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

List::Rank - Ranking of list elements

VERSION

This document describes version 0.004 of List::Rank (from Perl distribution List-Rank), released on 2023-11-23.

SYNOPSIS

Return the ranks of the elements if sorted numerically (note that equal values will be given equal ranks):

my @ranks = rank 10, 30, 20, 20; # => 1, 4, "2=", "2="

Return the ranks of the elements if sorted ascibetically:

my @ranks = rankstr "apricot", "cucumber", "banana", "banana"; # => 1,4,"2=","2="

Return the ranks of the elements if sorted by a custom sorter:

my @ranks = rankby {length(\$a) <=> length(\$b)}
"apricot", "cucumber", "banana", "banana"; # => 3, 4, "1=", "1="

Return the ranks of records (taken from Sort::Rank's Synopsis):

my @ranks = rankby {\$b->{score} <=> \$a->{score}} (
{   score   => 80,  name    => 'Andy'       },
{   score   => 70,  name    => 'Chrissie'   },
{   score   => 90,  name    => 'Alex'       },
{   score   => 90,  name    => 'Rosie'      },
{   score   => 80,  name    => 'Therese'    },
{   score   => 10,  name    => 'Mac'        },
{   score   => 10,  name    => 'Horton'     },
); # => ("3=", 5, "1=", "1=", "3=", "6=", "6=")

Sort the list numerically and return the elements as well as ranks in pairs:

my @res = sortrank 10, 30, 20, 20; # => 10,1, 20,"2=", 20,"2=", 30,4

Sort the list ascibetically and return the elements as well as ranks in pairs:

my @res = sortrankstr "apricot", "cucumber", "banana", "banana";
# => "apricot",1, "banana","2=", "banana","2=", "cucumber",4

Sort the list by a custom sorter and return the elements as well as ranks in pairs:

my @res = sortrankby {length(\$a) <=> length(\$b)} "apricot", "cucumber", "banana", "banana";
# => "banana","1=", "banana","1=", "apricot",3, "cucumber",4

HOMEPAGE

Please visit the project's homepage at https://metacpan.org/release/List-Rank.

SOURCE

Source repository is at https://github.com/perlancar/perl-List-Rank.

Sort::Rank also accomplishes the same thing, but by default it expects an arrayref *of hashrefs* with key score in each hashref. To process a list of scalars, you need to supply a coderef to supply the scores. Another thing is, the module does not provide a way to sort by a custom order.

AUTHOR

perlancar <perlancar@cpan.org>

CONTRIBUTING

To contribute, you can send patches by email/via RT, or send pull requests on GitHub.

Most of the time, you don't need to build the distribution yourself. You can simply modify the code, then test via:

% prove -l

If you want to build the distribution (e.g. to try to install it locally on your system), you can install Dist::Zilla, Dist::Zilla::PluginBundle::Author::PERLANCAR, Pod::Weaver::PluginBundle::Author::PERLANCAR, and sometimes one or two other Dist::Zilla- and/or Pod::Weaver plugins. Any additional steps required beyond that are considered a bug and can be reported to me.