The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Sort::Key::Top - select and sort top n elements

SYNOPSIS

  use Sort::Key::Top (nkeytop top);

  # select 5 first numbers by absolute value:
  @top = nkeytop { abs $_ } 5 => 1, 2, 7, 5, 5, 1, 78, 0, -2, -8, 2;
         # ==> @top = (1, 2, 1, 0, -2)

  # select 5 first words by lexicographic order:
  @a = qw(cat fish bird leon penguin horse rat elephant squirrel dog);
  @top = top 5 => @a;
         # ==> @top = qw(cat fish bird elephant dog);

DESCRIPTION

The functions available from this module select the top n elements from a list using several common orderings and custom key extraction procedures.

They are all variations around

  keytopsort { CALC_KEY($_) } $n => @data;

This function calculates the ordering key for every element in @data using the expression inside the block. Then it selects and orders the $n elements with the lower keys when compared lexicographically.

It is equivalent to the pure Perl expression:

  (sort { CALC_KEY($a) cmp CALC_KEY($b) } @data)[0 .. $n-1];

Variations allow to:

- use the own values as the ordering keys
  topsort 5 => qw(a b ab t uu g h aa aac);

     ==> a aa aac ab b
- return the selected values in the original order
  top 5 => qw(a b ab t uu g h aa aac);

     ==> a b ab aa aac
- use a different ordering

For instance comparing the keys as numbers, using the locale configuration or in reverse order:

  rnkeytop { length $_ } 3 => qw(a ab aa aac b t uu g h);

     ==> ab aa aac

  rnkeytopsort { length $_ } 3 => qw(a ab aa aac b t uu g h);

     ==> aac ab aa

A prefix is used to indicate the required ordering:

(no prefix)

lexicographical ascending order

r

lexicographical descending order

l

lexicographical ascending order obeying locale configuration

r

lexicographical descending order obeying locale configuration

n

numerical ascending order

rn

numerical descending order

i

numerical ascending order but converting the keys to integers first

ri

numerical descending order but converting the keys to integers first

u

numerical ascending order but converting the keys to unsigned integers first

ru

numerical descending order but converting the keys to unsigned integers first

The full list of available functions is:

  top ltop ntop itop utop rtop rltop rntop ritop rutop

  keytop lkeytop nkeytop ikeytop ukeytop rkeytop rlkeytop rnkeytop
  rikeytop rukeytop

  topsort ltopsort ntopsort itopsort utopsort rtopsort rltopsort
  rntopsort ritopsort rutopsort

  keytopsort lkeytopsort nkeytopsort ikeytopsort ukeytopsort
  rkeytopsort rlkeytopsort rnkeytopsort rikeytopsort rukeytopsort

SEE ALSO

Sort::Key, "sort" in perlfunc.

The Wikipedia article about selection algorithms http://en.wikipedia.org/wiki/Selection_algorithm.

COPYRIGHT AND LICENSE

Copyright (C) 2006 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.8 or, at your option, any later version of Perl 5 you may have available.