Sort::SubList - Sort only certain elements in a list, while maintaining the order of the rest
This document describes version 0.001 of Sort::SubList (from Perl distribution Sort-SubList), released on 2019-12-14.
use Sort::SubList qw(sort_sublist); my @sorted = sort_sublist sub { length($_[0]) <=> length($_[1]) }, # comparison routine sub { /\D/ }, # element selection routine "quux", 12, 1, "us", 400, 3, "a", "foo"; # => ("a", 12, 1, "us", 400, 3, "foo", "quux")
This module provides "sort_sublist" routine to sort only certain elements in a list, while keeping the order of the rest of the elements intact (in the original position). So basically what this routine does is to grep the elements to be sorted, record their positions, sort these elements, and put them back to the recorded positions.
Usage:
my @sorted = sort_sublist $comparison_sub, $filter_sub, @list;
sort_sublist
sort
The builtin sort's behavior is hard to emulate with subroutine prototypes. For more discussion: https://www.perlmonks.org/index.pl/www.mrtg.org?node_id=1207981. For simplicity, I do away with prototypes altogether.
Something like this will do:
sub { no strict 'refs'; my $caller = caller(); my $a = @_ ? $_[0] : ${"$caller\::a"}; my $b = @_ ? $_[1] : ${"$caller\::b"}; # compare $a and $b ... }
Or, you can just use $_[0] (instead of $a) and $_[1] (instead of $b) like the example in Synopsis shows. Again, this is where the specialness of the sort subroutine is not easy or straightforward to emulate.
$_[0]
$a
$_[1]
$b
Please visit the project's homepage at https://metacpan.org/release/Sort-SubList.
Source repository is at https://github.com/perlancar/perl-Sort-SubList.
Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=Sort-SubList
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.
perlancar <perlancar@cpan.org>
This software is copyright (c) 2019 by 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.
To install Sort::SubList, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Sort::SubList
CPAN shell
perl -MCPAN -e shell install Sort::SubList
For more information on module installation, please visit the detailed CPAN module installation guide.