SortKey - Reusable sort key generators
0.1
This document describes version 0.1.1 of SortKey (from Perl distribution SortKey), released on 2024-02-14.
Basic use with sort():
sort()
use SortKey::Num::length; my $by_length = SortKey::Num::length::gen_keygen; my @sorted = sort { $by_length->($a) <=> $by_length->($b) } "food", "foo", "foolish"; # => ('foo', 'food', 'foolish')
Basic use with Sort::Key:
Sort::Key
use Sort::Key qw(nkeysort); use SortKey::Num::length; my $by_length = SortKey::Num::length::gen_keygen; # use & prefix to get around prototype restriction my @sorted = &nkeysort($by_length, "food", "foo", "foolish"); # => ('foo', 'food', 'foolish')
Specifying arguments:
use Sort::Key qw(nkeysort); use SortKey::Num::pattern_count; my $by_pattern_count = Sort::Key::Num::pattern_count::gen_keygen(pattern => qr/foo/); my @sorted = &nkeysort($by_pattern_count, ...);
Specifying comparer on the command-line (for other CLI's):
% customsort -k length ... % customsort -c pattern_count=pattern,foo ...
EXPERIMENTAL. SPEC MIGHT STILL CHANGE.
A sort key generator is a subroutine that accepts an item and converts it to a string/numeric key. The key then can be compared using <=> or cmp operator.
<=>
cmp
A SortKey::* module is a module that can return a sort key generator.
package SorKey::Num::pattern_count; # required. must return a hash (DefHash) sub meta { return +{ v => 1, summary => 'Number of occurrences of a pattern, as sort key', args => { pattern => { schema => 're_from_str*', # Sah schema req => 1, }, }, }; } sub gen_keygen { my %args = @_; ... } 1;
SortKey::Num::* are for modules that return a numeric key generator. Other subnamespaces are for modules that return a string key.
SortKey::Num::*
Please visit the project's homepage at https://metacpan.org/release/SortKey.
Source repository is at https://github.com/perlancar/perl-SortKey.
DefHash
Sah
Sorter
Comparer
Sort::Sub
Sorter, SortKey, and Comparer are meant to eventually supersede Sort::Sub. The main improvement over Sort::Sub is the split into three kinds of subroutines:
SortKey
A subroutine that accepts a list of items to sort.
Sorter::* modules are meant to generate sorters.
Sorter::*
A subroutine that converts an item to a string/numeric key suitable for simple comparison using eq or == during sorting.
eq
==
SortKey::* modules are meant to generate sort key generators.
SortKey::*
A subroutine that compares two items. Can be used in sort() as custom sort block.
Comparer::* modules are meant to generate comparers.
Comparer::*
Perl's sort(), as mentioned above, allows us to specify a comparer, but oftentimes it's more efficient to sort by key using key generator, where the keys are often cached. And sometimes due to preprocessing and/or postprocessing it's more suitable to use the more generic sorter interface.
Aside from the above, SortKey also makes Sort::Sub's special arguments reverse and is_ci become ordinary arguments, because they are not always applicable in all situation, especially is_ci.
reverse
is_ci
perlancar <perlancar@cpan.org>
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.
This software is copyright (c) 2024 by perlancar <perlancar@cpan.org>.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=SortKey
When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.
To install SortKey, copy and paste the appropriate command in to your terminal.
cpanm
cpanm SortKey
CPAN shell
perl -MCPAN -e shell install SortKey
For more information on module installation, please visit the detailed CPAN module installation guide.