my @sorted = sort { split_sort( fc($a), fc($b), 'type', 'split expr') } @array;
# I made changes to split_sort detailed in a moment.
####
sub split_sort {
my ($in_a, $in_b, $sort_type, $split) = @_;
if ($sort_type =~ /^(alpha|letter)/) {
$in_a cmp $in_b
}
else {
$split = qr($split);
my ($numa1, $numa2) = split(/$split/, $in_a, 2);
my ($numb1, $numb2) = split(/$split/, $in_b, 2);
if ($sort_type =~ /^num/) {
$numa1 <=> $numb1 || $numa2 <=> $numb2
}
elsif (fc($sort_type) eq 'left' ) {
$numa1 <=> $numb1 || $numa2 cmp $numb2
}
elsif (fc($sort_type) eq 'right' ) {
$numa1 cmp $numb1 || $numa2 <=> $numb2
}
}
}
##
##
=pod
=encoding utf8
=head1 NAME
B returns the expression to split the values in lists for sort.
=head1 VERSION
This document describes Fancy::Sort::Split version 1.0.
=head1 SYNOPSIS
my @numbers = qw(1:2 1:02 3:4 5:78 50:89 10:5);
my @split_sorted = sort { split_sort($a, $b, 'number', ':') } @numbers;
# returns
# [
# '1:2',
# '1:02',
# '3:4',
# '5:78',
# '10:5',
# '50:89'
# ];
my @left = qw(2:a 02:a 4:a 28:a 89:a 5:a);
my @split_sorted_left = sort { split_sort($a, $b, 'left', ':') } @left;
# returns
# [
# '2:a',
# '02:a',
# '4:a',
# '5:a',
# '28:a',
# '89:a'
# ];
my @right = qw(a:2 a:02 a:4 a:28 a:89 a:5);
my @split_sorted_right = sort { split_sort($a, $b, 'right', ':') } @right;
# returns
# [
# 'a:2',
# 'a:02',
# 'a:4',
# 'a:5',
# 'a:28',
# 'a:89'
# ];
=head1 DESCRIPTION
Fancy::Sort::Split returns the expression to split the values in lists for L subroutines using C. C has to be imported into your script.
C has four required parameters. The first and second paremeters are C<$a> and C<$b> from C or C<$b> and C<$a> if you want a descending sort. The third parameter is the expression you want to split the strings by. The fourth is the type of sort you want, C or C (C).
split_sort($a, $b, 'type', 'expr');
A note of caution for the numerical sorts, when a number has a leading zero (C<02>), the leading zero will be dropped. So, C<02> will be the same as C<2>.
It requires Perl version 5.16.0 or better.
=head2 Numerical sort
When you have numbers on both sides of the expression, use C so the numbers on both sides are numerically sorted.
split_sort($a, $b, 'number', 'expr');
=head2 Numerical sort on the left
When you have numbers on the left side of the expression, use C so the numbers on the left side are numerically sorted..
split_sort($a, $b, 'left', 'expr');
=head2 Numberial sort on the right
When you have numbers on the right side of the expression, use C so the numbers on the right side are numerically sorted..
split_sort($a, $b, 'right', 'expr');
=head2 Alphabetical sort
When you have letters on both sides of the expression, use C or C. However, the alphabetical sort is redundant and was added for completeness. The sort expression returned will be the same as C<$a cmp $b> for the entire string. So, for alphabetical sorts, you may omit the expression for the split.
split_sort($a, $b, 'alpha', 'expr');
split_sort($a, $b, 'letter', 'expr');
=head1 DEPENDENCIES
Fancy::Sort::Split depends on L.
=head1 AUTHOR
Lady Aleena
=cut