Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

RFC: Simple switches for 'sort' and list 'reverse'

by rsFalse (Chaplain)
on Mar 05, 2021 at 16:32 UTC ( #11129169=perlmeditation: print w/replies, xml ) Need Help??

Upd. I slightly changed a node name and rewrote a section about reverse (older text is under strikethrough and moved to the end of the post).

Rarely we want to do something the same with both: with original and reversed list, or with original and sorted (and sorted backwards) list. Here you will find few examples how to use a simple switch to turn ON/OFF sorting or reversing list.

sort: Say we want to apply some function on original, sorted, and sorted backwards list. Example program:
#!/usr/bin/perl use warnings; use strict; $\ = $/; sub sort_sub{ return $a cmp $b; } sub some_sub{ my $ref = shift; print "@{ $ref }"; for my $i ( 0 .. @{ $ref } - 2 ){ $ref->[ $i ] cmp $ref->[ $i + 1 ] and do { ;;; }; } }
my @original = ( 5, 'A' .. 'C', 0, 4 ); my @sorted = sort sort_sub @original; my @r_sorted = reverse @sorted; some_sub( $_ ) for \@original, \@sorted, \@r_sorted; print '-';
Now we use multiplicative "switch" to gain the same output:
some_sub( [ sort { $_ * sort_sub } @original ] ) for 0, 1, -1; print '-';
Note we avoided intermediate variables.
OUTPUT:
5 A B C 0 4 0 4 5 A B C C B A 5 4 0 - 5 A B C 0 4 0 4 5 A B C C B A 5 4 0 -
Explanation. Sort subroutine returns -1, 0 or 1 on each comparison of $a and $b. In case we multiply by -1, we invert the result; in case with 1 we don't change anything; in case with 0 we convert all comparison to "equals", so sorting algorithm are not to change ordering of list elements.

reverse: Say we want to apply some function on original and reversed list. We can simply pass 0 or 1 into sort block:
my @reversed = reverse @original; some_sub( $_ ) for \@original, \@reversed; print '-'; some_sub( [ sort { $_ } @original ] ) for 0, 1;
OUTPUT:
5 A B C 0 4 4 0 C B A 5 - 5 A B C 0 4 4 0 C B A 5
Explanation. No matter how each of two elements are compared, we assign a 1 or 0 to a comparison result. If 0, then algorithm is to do nothing. If 1, then algorithm reverse a pair of elements. It seems to works fine with Perl's mergesort (which is by default since v5.8.0)*. If -1, then our list becomes kinda shuffled with some pattern.**

*Upd. Although documentation of sort pragma (e.g. 5.26.3/sort) was not clear about which sort algorithm is default saying "...but in Perl 5.8 a mergesort algorithm was also made available..." while talking mostly about quicksort, but meanwhile perlsec (e.g. 5.26.3/perlsec) says strictly: "Starting from Perl 5.8.0 a different sorting algorithm, mergesort, is used by default."

Upd. Have in mind that reversing a huge list with 'sort {1} list' may take a bit more time*** instead of simply reversing with O(N) time.

Upd.** It seems that 'sort {-1} list' shuffles list elements to some pattern IF the length of the list is between 3 and 17! Otherwise it doesn't shuffle, i.e. outputs the same as 'sort {0} list' does.

Upd.*** 'sort { $_ } list' where $_ is 1, 0 or -1 behaves linearly in O(N).

Here is the code which I used for finding how many iterations sort uses and does 'sort {-1} list' shuffle elements or not:
srand 1; my $show_distance = 10; for my $length ( 1 .. 3 ** 3 - 1, map { 3 ** $_, 3 ** $_ + 1 } 3 .. 10, ){ print join " length: $length ", ( '=' x 5 ) x 2; my $test = [ map int rand 10, 1 .. $length ]; my %sub = ( '$a <=> $b' => { 'sub' => sub { $a <=> $b } }, '0' => { 'sub' => sub { 0 } }, '1' => { 'sub' => sub { 1 } }, '-1' => { 'sub' => sub { -1 } }, ); for my $key ( sort keys %sub ){ my $count = 0; $sub{ $key }{ 'array' } = [ sort { $count ++; $sub{ $key }{ 'sub' }->() } @{ $test } + ]; $sub{ $key }{ 'count' } = $count; } for my $key ( sort keys %sub ){ print "\$key:[$key]"; print " count:[$sub{ $key }{ 'count' }]"; my $len = @{ $sub{ $key }{ 'array' } }; printf " array:[%s ... %s]\n", ( join ' ', @{ $sub{ $key }{ 'array' } }[ grep { 0 || $show_distance > abs( $_ - 0 ) } 0 .. $len - 1 ] ), ( join ' ', @{ $sub{ $key }{ 'array' } }[ grep { 0 || $show_distance > abs( $_ - $len + 1 ) && not $show_distance > abs( $_ - 0 ) } 0 .. $len - 1 ] ), ; } print "key='1' NOT EQUAL to reverse!" if "@{ $sub{ 1 }{ 'array' } +}" ne join ' ', reverse @{ $test }; print "key='-1' NOT EQUAL to key='0'!" if "@{ $sub{ -1 }{ 'array' +} }" ne "@{ $sub{ 0 }{ 'array' } }"; print ''; }
OUTPUT:
===== length: 1 ===== $key:[$a <=> $b] count:[0] array:[0 ... ] $key:[-1] count:[0] array:[0 ... ] $key:[0] count:[0] array:[0 ... ] $key:[1] count:[0] array:[0 ... ] ===== length: 2 ===== $key:[$a <=> $b] count:[1] array:[4 8 ... ] $key:[-1] count:[1] array:[4 8 ... ] $key:[0] count:[1] array:[4 8 ... ] $key:[1] count:[1] array:[8 4 ... ] ===== length: 3 ===== $key:[$a <=> $b] count:[2] array:[0 3 5 ... ] $key:[-1] count:[3] array:[3 0 5 ... ] $key:[0] count:[3] array:[3 5 0 ... ] $key:[1] count:[2] array:[0 5 3 ... ] key='-1' NOT EQUAL to key='0'! ===== length: 4 ===== $key:[$a <=> $b] count:[5] array:[1 3 7 9 ... ] $key:[-1] count:[5] array:[1 7 9 3 ... ] $key:[0] count:[4] array:[1 9 7 3 ... ] $key:[1] count:[4] array:[3 7 9 1 ... ] key='-1' NOT EQUAL to key='0'! ===== length: 5 ===== $key:[$a <=> $b] count:[8] array:[0 1 3 5 9 ... ] $key:[-1] count:[7] array:[3 9 1 5 0 ... ] $key:[0] count:[8] array:[3 5 1 0 9 ... ] $key:[1] count:[5] array:[9 0 1 5 3 ... ] key='-1' NOT EQUAL to key='0'! ===== length: 6 ===== $key:[$a <=> $b] count:[11] array:[1 1 2 5 6 8 ... ] $key:[-1] count:[10] array:[1 1 2 6 5 8 ... ] $key:[0] count:[9] array:[1 5 2 8 1 6 ... ] $key:[1] count:[7] array:[6 1 8 2 5 1 ... ] key='-1' NOT EQUAL to key='0'! ===== length: 7 ===== $key:[$a <=> $b] count:[13] array:[0 2 3 5 5 8 9 ... ] $key:[-1] count:[14] array:[2 3 5 5 0 8 9 ... ] $key:[0] count:[11] array:[2 0 5 9 3 8 5 ... ] $key:[1] count:[9] array:[5 8 3 9 5 0 2 ... ] key='-1' NOT EQUAL to key='0'! ===== length: 8 ===== $key:[$a <=> $b] count:[16] array:[0 1 2 3 6 8 8 9 ... ] $key:[-1] count:[17] array:[3 9 6 1 8 0 2 8 ... ] $key:[0] count:[12] array:[3 8 6 2 9 0 1 8 ... ] $key:[1] count:[12] array:[8 1 0 9 2 6 8 3 ... ] key='-1' NOT EQUAL to key='0'! ===== length: 9 ===== $key:[$a <=> $b] count:[21] array:[1 2 3 4 4 5 6 6 7 ... ] $key:[-1] count:[19] array:[1 3 4 6 2 4 5 7 6 ... ] $key:[0] count:[18] array:[1 7 2 6 4 5 3 4 6 ... ] $key:[1] count:[12] array:[6 4 3 5 4 6 2 7 1 ... ] key='-1' NOT EQUAL to key='0'! ===== length: 10 ===== $key:[$a <=> $b] count:[25] array:[1 3 3 6 6 7 7 8 8 9 ... ] $key:[-1] count:[23] array:[7 1 9 6 8 8 6 3 7 3 ... ] $key:[0] count:[19] array:[7 7 8 3 9 6 1 8 6 3 ... ] $key:[1] count:[15] array:[3 6 8 1 6 9 3 8 7 7 ... ] key='-1' NOT EQUAL to key='0'! ===== length: 11 ===== $key:[$a <=> $b] count:[27] array:[0 2 2 2 4 5 5 5 5 6 ... 9] $key:[-1] count:[27] array:[5 6 2 0 2 5 5 5 4 2 ... 9] $key:[0] count:[23] array:[5 4 2 9 2 5 6 5 5 2 ... 0] $key:[1] count:[17] array:[0 2 5 5 6 5 2 9 2 4 ... 5] key='-1' NOT EQUAL to key='0'! ===== length: 12 ===== $key:[$a <=> $b] count:[30] array:[1 2 3 5 5 5 6 6 6 7 ... 7 9] $key:[-1] count:[31] array:[2 5 6 1 7 6 3 5 7 5 ... 6 9] $key:[0] count:[24] array:[2 7 7 6 6 3 5 5 6 9 ... 1 5] $key:[1] count:[20] array:[5 1 9 6 5 5 3 6 6 7 ... 7 2] key='-1' NOT EQUAL to key='0'! ===== length: 13 ===== $key:[$a <=> $b] count:[34] array:[0 2 2 2 3 3 5 5 6 8 ... 9 9 9] $key:[-1] count:[34] array:[0 9 2 2 2 9 3 6 5 3 ... 8 9 5] $key:[0] count:[28] array:[0 5 2 9 2 8 3 5 9 6 ... 9 3 2] $key:[1] count:[22] array:[2 3 9 6 9 5 3 8 2 9 ... 2 5 0] key='-1' NOT EQUAL to key='0'! ===== length: 14 ===== $key:[$a <=> $b] count:[39] array:[1 3 3 3 4 5 5 5 6 8 ... 9 9 9 9] $key:[-1] count:[39] array:[4 3 3 6 3 5 5 5 9 1 ... 9 9 8 9] $key:[0] count:[29] array:[4 9 3 8 3 9 5 9 3 1 ... 5 9 6 5] $key:[1] count:[25] array:[5 6 9 5 1 3 9 5 9 3 ... 8 3 9 4] key='-1' NOT EQUAL to key='0'! ===== length: 15 ===== $key:[$a <=> $b] count:[42] array:[0 0 1 1 2 3 6 6 6 8 ... 8 8 9 9 9] $key:[-1] count:[45] array:[9 2 8 6 9 9 0 8 3 1 ... 6 0 6 8 1] $key:[0] count:[31] array:[9 3 9 6 8 6 0 1 2 1 ... 9 8 6 0 8] $key:[1] count:[28] array:[8 0 6 8 9 1 2 1 0 6 ... 8 6 9 3 9] key='-1' NOT EQUAL to key='0'! ===== length: 16 ===== $key:[$a <=> $b] count:[47] array:[1 2 2 2 3 4 4 5 5 7 ... 7 7 8 9 9 9] $key:[-1] count:[49] array:[2 8 1 5 3 2 9 9 7 4 ... 2 5 9 4 7 7] $key:[0] count:[32] array:[2 7 3 9 1 2 9 7 8 4 ... 2 4 5 5 9 7] $key:[1] count:[32] array:[7 9 5 5 4 2 4 8 7 9 ... 2 1 9 3 7 2] key='-1' NOT EQUAL to key='0'! ===== length: 17 ===== $key:[$a <=> $b] count:[54] array:[0 1 2 2 2 2 4 4 4 4 ... 4 5 6 6 7 7 8] $key:[-1] count:[51] array:[7 2 5 7 4 4 4 6 2 4 ... 8 2 1 2 6 0 4] $key:[0] count:[39] array:[7 0 2 4 4 1 5 8 4 6 ... 2 4 4 2 7 2 6] $key:[1] count:[31] array:[6 2 7 2 4 4 2 6 4 8 ... 5 1 4 4 2 0 7] key='-1' NOT EQUAL to key='0'! ===== length: 18 ===== $key:[$a <=> $b] count:[57] array:[0 1 1 2 2 2 3 3 3 3 ... 5 5 6 6 6 6 7 9] $key:[-1] count:[17] array:[6 3 0 2 3 9 5 2 6 1 ... 1 6 5 6 2 3 7 3] $key:[0] count:[17] array:[6 3 0 2 3 9 5 2 6 1 ... 1 6 5 6 2 3 7 3] $key:[1] count:[17] array:[3 7 3 2 6 5 6 1 1 6 ... 2 5 9 3 2 0 3 6] ===== length: 19 ===== $key:[$a <=> $b] count:[60] array:[0 0 2 2 3 3 3 4 4 5 ... 5 7 7 7 7 8 8 9 9] $key:[-1] count:[18] array:[8 4 7 2 7 3 7 3 0 5 ... 9 0 2 9 8 4 7 3 5] $key:[0] count:[18] array:[8 4 7 2 7 3 7 3 0 5 ... 9 0 2 9 8 4 7 3 5] $key:[1] count:[18] array:[5 3 7 4 8 9 2 0 9 5 ... 0 3 7 3 7 2 7 4 8] ===== length: 20 ===== $key:[$a <=> $b] count:[66] array:[0 1 1 2 2 2 3 3 3 4 ... 4 5 5 5 6 7 7 7 7 9] $key:[-1] count:[19] array:[5 2 5 4 9 2 7 3 2 3 ... 7 6 5 7 1 4 0 3 1 7] $key:[0] count:[19] array:[5 2 5 4 9 2 7 3 2 3 ... 7 6 5 7 1 4 0 3 1 7] $key:[1] count:[19] array:[7 1 3 0 4 1 7 5 6 7 ... 3 2 3 7 2 9 4 5 2 5] ===== length: 21 ===== $key:[$a <=> $b] count:[68] array:[0 0 0 2 3 4 4 5 5 5 ... 5 6 6 7 8 8 9 9 9 9] $key:[-1] count:[20] array:[9 5 9 4 8 5 5 9 4 7 ... 3 9 8 2 6 0 5 0 5 0] $key:[0] count:[20] array:[9 5 9 4 8 5 5 9 4 7 ... 3 9 8 2 6 0 5 0 5 0] $key:[1] count:[20] array:[0 5 0 5 0 6 2 8 9 3 ... 7 4 9 5 5 8 4 9 5 9] ===== length: 22 ===== $key:[$a <=> $b] count:[71] array:[0 0 0 0 1 2 2 3 3 3 ... 4 4 5 6 6 8 8 8 8 9] $key:[-1] count:[21] array:[8 0 8 4 4 5 0 6 6 3 ... 0 8 0 9 1 8 2 3 3 4] $key:[0] count:[21] array:[8 0 8 4 4 5 0 6 6 3 ... 0 8 0 9 1 8 2 3 3 4] $key:[1] count:[21] array:[4 3 3 2 8 1 9 0 8 0 ... 3 6 6 0 5 4 4 8 0 8] ===== length: 23 ===== $key:[$a <=> $b] count:[79] array:[0 0 1 1 3 3 4 5 5 5 ... 7 8 8 8 8 9 9 9 9 9] $key:[-1] count:[22] array:[5 5 1 9 9 3 5 8 9 8 ... 9 6 7 9 3 4 5 1 8 0] $key:[0] count:[22] array:[5 5 1 9 9 3 5 8 9 8 ... 9 6 7 9 3 4 5 1 8 0] $key:[1] count:[22] array:[0 8 1 5 4 3 9 7 6 9 ... 8 9 8 5 3 9 9 1 5 5] ===== length: 24 ===== $key:[$a <=> $b] count:[81] array:[0 0 0 0 0 1 2 2 3 3 ... 5 6 6 6 7 7 7 7 8 9] $key:[-1] count:[23] array:[0 6 5 3 6 7 4 0 4 7 ... 0 6 2 9 0 4 4 8 7 7] $key:[0] count:[23] array:[0 6 5 3 6 7 4 0 4 7 ... 0 6 2 9 0 4 4 8 7 7] $key:[1] count:[23] array:[7 7 8 4 4 0 9 2 6 0 ... 7 4 0 4 7 6 3 5 6 0] ===== length: 25 ===== $key:[$a <=> $b] count:[95] array:[0 1 1 1 2 2 2 2 2 2 ... 5 5 5 5 6 6 6 8 8 8] $key:[-1] count:[24] array:[8 2 4 1 4 2 3 2 5 1 ... 5 2 1 2 6 4 3 2 6 8] $key:[0] count:[24] array:[8 2 4 1 4 2 3 2 5 1 ... 5 2 1 2 6 4 3 2 6 8] $key:[1] count:[24] array:[8 6 2 3 4 6 2 1 2 5 ... 1 5 2 3 2 4 1 4 2 8] ===== length: 26 ===== $key:[$a <=> $b] count:[90] array:[0 1 1 1 2 2 2 3 3 4 ... 6 6 6 7 7 7 8 8 8 9] $key:[-1] count:[25] array:[0 2 8 7 6 6 6 2 1 7 ... 4 5 4 3 7 9 1 2 6 8] $key:[0] count:[25] array:[0 2 8 7 6 6 6 2 1 7 ... 4 5 4 3 7 9 1 2 6 8] $key:[1] count:[25] array:[8 6 2 1 9 7 3 4 5 4 ... 7 1 2 6 6 6 7 8 2 0] ===== length: 27 ===== $key:[$a <=> $b] count:[99] array:[0 0 0 0 0 1 1 2 3 3 ... 7 7 7 7 8 8 8 9 9 9] $key:[-1] count:[26] array:[0 3 0 7 4 7 1 9 7 3 ... 6 8 7 0 0 0 3 6 1 6] $key:[0] count:[26] array:[0 3 0 7 4 7 1 9 7 3 ... 6 8 7 0 0 0 3 6 1 6] $key:[1] count:[26] array:[6 1 6 3 0 0 0 7 8 6 ... 3 7 9 1 7 4 7 0 3 0] ===== length: 28 ===== $key:[$a <=> $b] count:[101] array:[0 0 0 1 1 1 1 1 2 2 ... 6 6 6 7 7 8 8 9 9 9] $key:[-1] count:[27] array:[1 1 3 1 6 0 8 4 8 2 ... 9 6 3 3 5 9 7 7 5 6] $key:[0] count:[27] array:[1 1 3 1 6 0 8 4 8 2 ... 9 6 3 3 5 9 7 7 5 6] $key:[1] count:[27] array:[6 5 7 7 9 5 3 3 6 9 ... 2 8 4 8 0 6 1 3 1 1] ===== length: 81 ===== $key:[$a <=> $b] count:[415] array:[0 0 0 0 0 0 0 0 0 1 ... 8 8 9 9 9 9 9 9 9 9] $key:[-1] count:[80] array:[8 3 5 8 0 6 1 0 0 4 ... 0 9 1 9 6 3 1 4 9 4] $key:[0] count:[80] array:[8 3 5 8 0 6 1 0 0 4 ... 0 9 1 9 6 3 1 4 9 4] $key:[1] count:[80] array:[4 9 4 1 3 6 9 1 9 0 ... 4 0 0 1 6 0 8 5 3 8] ===== length: 82 ===== $key:[$a <=> $b] count:[421] array:[0 0 0 0 0 0 0 0 0 1 ... 8 8 8 8 9 9 9 9 9 9] $key:[-1] count:[81] array:[1 5 8 6 0 2 8 7 6 1 ... 1 9 1 5 9 7 0 4 9 2] $key:[0] count:[81] array:[1 5 8 6 0 2 8 7 6 1 ... 1 9 1 5 9 7 0 4 9 2] $key:[1] count:[81] array:[2 9 4 0 7 9 5 1 9 1 ... 1 6 7 8 2 0 6 8 5 1] ===== length: 243 ===== $key:[$a <=> $b] count:[1572] array:[0 0 0 0 0 0 0 0 0 0 ... 9 9 9 9 9 9 9 9 9 9] $key:[-1] count:[242] array:[2 4 4 6 3 0 5 3 2 2 ... 1 2 5 5 8 9 4 6 7 7] $key:[0] count:[242] array:[2 4 4 6 3 0 5 3 2 2 ... 1 2 5 5 8 9 4 6 7 7] $key:[1] count:[242] array:[7 7 6 4 9 8 5 5 2 1 ... 2 2 3 5 0 3 6 4 4 2] ===== length: 244 ===== $key:[$a <=> $b] count:[1590] array:[0 0 0 0 0 0 0 0 0 0 ... 9 9 9 9 9 9 9 9 9 9] $key:[-1] count:[243] array:[8 5 7 8 8 6 7 9 1 4 ... 6 7 4 8 2 4 5 0 5 3] $key:[0] count:[243] array:[8 5 7 8 8 6 7 9 1 4 ... 6 7 4 8 2 4 5 0 5 3] $key:[1] count:[243] array:[3 5 0 5 4 2 8 4 7 6 ... 4 1 9 7 6 8 8 7 5 8] ===== length: 729 ===== $key:[$a <=> $b] count:[5321] array:[0 0 0 0 0 0 0 0 0 0 ... 9 9 9 9 9 9 9 9 9 9] $key:[-1] count:[728] array:[4 2 9 5 7 4 2 1 3 6 ... 2 9 6 0 4 9 4 3 0 3] $key:[0] count:[728] array:[4 2 9 5 7 4 2 1 3 6 ... 2 9 6 0 4 9 4 3 0 3] $key:[1] count:[728] array:[3 0 3 4 9 4 0 6 9 2 ... 6 3 1 2 4 7 5 9 2 4] ===== length: 730 ===== $key:[$a <=> $b] count:[5384] array:[0 0 0 0 0 0 0 0 0 0 ... 9 9 9 9 9 9 9 9 9 9] $key:[-1] count:[729] array:[1 2 5 2 9 6 6 0 4 9 ... 4 7 1 8 2 6 5 9 5 9] $key:[0] count:[729] array:[1 2 5 2 9 6 6 0 4 9 ... 4 7 1 8 2 6 5 9 5 9] $key:[1] count:[729] array:[9 5 9 5 6 2 8 1 7 4 ... 9 4 0 6 6 9 2 5 2 1] ===== length: 2187 ===== $key:[$a <=> $b] count:[16600] array:[0 0 0 0 0 0 0 0 0 0 ... 9 9 9 9 9 9 9 9 9 9] $key:[-1] count:[2186] array:[0 0 9 3 2 1 0 7 9 7 ... 0 2 4 8 7 2 2 5 3 6] $key:[0] count:[2186] array:[0 0 9 3 2 1 0 7 9 7 ... 0 2 4 8 7 2 2 5 3 6] $key:[1] count:[2186] array:[6 3 5 2 2 7 8 4 2 0 ... 7 9 7 0 1 2 3 9 0 0] ===== length: 2188 ===== $key:[$a <=> $b] count:[16606] array:[0 0 0 0 0 0 0 0 0 0 ... 9 9 9 9 9 9 9 9 9 9] $key:[-1] count:[2187] array:[7 0 8 6 8 3 0 9 0 4 ... 7 5 7 7 0 6 7 9 9 3] $key:[0] count:[2187] array:[7 0 8 6 8 3 0 9 0 4 ... 7 5 7 7 0 6 7 9 9 3] $key:[1] count:[2187] array:[3 9 9 7 6 0 7 7 5 7 ... 4 0 9 0 3 8 6 8 0 7] ===== length: 6561 ===== $key:[$a <=> $b] count:[50834] array:[0 0 0 0 0 0 0 0 0 0 ... 9 9 9 9 9 9 9 9 9 9] $key:[-1] count:[6560] array:[5 6 1 7 9 9 7 6 7 6 ... 0 2 8 1 0 2 7 6 8 4] $key:[0] count:[6560] array:[5 6 1 7 9 9 7 6 7 6 ... 0 2 8 1 0 2 7 6 8 4] $key:[1] count:[6560] array:[4 8 6 7 2 0 1 8 2 0 ... 6 7 6 7 9 9 7 1 6 5] ===== length: 6562 ===== $key:[$a <=> $b] count:[50755] array:[0 0 0 0 0 0 0 0 0 0 ... 9 9 9 9 9 9 9 9 9 9] $key:[-1] count:[6561] array:[6 6 3 8 5 3 5 1 6 1 ... 8 5 0 4 1 3 2 0 9 9] $key:[0] count:[6561] array:[6 6 3 8 5 3 5 1 6 1 ... 8 5 0 4 1 3 2 0 9 9] $key:[1] count:[6561] array:[9 9 0 2 3 1 4 0 5 8 ... 1 6 1 5 3 5 8 3 6 6] ===== length: 19683 ===== $key:[$a <=> $b] count:[152997] array:[0 0 0 0 0 0 0 0 0 0 ... 9 9 9 9 9 9 9 9 9 9] $key:[-1] count:[19682] array:[0 2 6 1 7 8 3 3 7 6 ... 6 4 2 3 4 0 7 8 6 7] $key:[0] count:[19682] array:[0 2 6 1 7 8 3 3 7 6 ... 6 4 2 3 4 0 7 8 6 7] $key:[1] count:[19682] array:[7 6 8 7 0 4 3 2 4 6 ... 6 7 3 3 8 7 1 6 2 0] ===== length: 19684 ===== $key:[$a <=> $b] count:[152923] array:[0 0 0 0 0 0 0 0 0 0 ... 9 9 9 9 9 9 9 9 9 9] $key:[-1] count:[19683] array:[0 4 2 3 6 9 6 7 3 2 ... 3 4 0 5 2 7 7 2 6 9] $key:[0] count:[19683] array:[0 4 2 3 6 9 6 7 3 2 ... 3 4 0 5 2 7 7 2 6 9] $key:[1] count:[19683] array:[9 6 2 7 7 2 5 0 4 3 ... 2 3 7 6 9 6 3 2 4 0] ===== length: 59049 ===== $key:[$a <=> $b] count:[459848] array:[0 0 0 0 0 0 0 0 0 0 ... 9 9 9 9 9 9 9 9 9 9] $key:[-1] count:[59048] array:[2 7 6 7 1 7 0 9 0 7 ... 5 1 5 0 1 5 7 5 9 9] $key:[0] count:[59048] array:[2 7 6 7 1 7 0 9 0 7 ... 5 1 5 0 1 5 7 5 9 9] $key:[1] count:[59048] array:[9 9 5 7 5 1 0 5 1 5 ... 7 0 9 0 7 1 7 6 7 2] ===== length: 59050 ===== $key:[$a <=> $b] count:[460469] array:[0 0 0 0 0 0 0 0 0 0 ... 9 9 9 9 9 9 9 9 9 9] $key:[-1] count:[59049] array:[9 6 3 2 4 5 6 9 2 4 ... 0 1 5 0 5 5 4 4 0 9] $key:[0] count:[59049] array:[9 6 3 2 4 5 6 9 2 4 ... 0 1 5 0 5 5 4 4 0 9] $key:[1] count:[59049] array:[9 0 4 4 5 5 0 5 1 0 ... 4 2 9 6 5 4 2 3 6 9]
##############
RFC: Simple switches for 'sort' and numeric list 'reverse'

reverse: This trick seems to work only on lists of unique numeric values. Say we want to apply some function on original and reversed list. I'm not sure how, but sorting by $a + $b reverses a list. So we can pass 0 or 1 to multiply its result:
tr/A-C/1-3/ for @original; my @reversed = reverse @original; some_sub( $_ ) for \@original, \@reversed; print '-'; some_sub( [ sort { $_ * ( $a + $b ) } @original ] ) for 0, 1;
OUTPUT:
5 1 2 3 0 4 4 0 3 2 1 5 - 5 1 2 3 0 4 4 0 3 2 1 5
In case if list elements aren't unique, 'sort' sometimes returns incorrect but similar to expected results, e.g.:
original: 7 1 0 9 8 0 7 7 8 0 reversed: 0 8 7 7 0 8 9 0 1 7 $a + $b : 0 8 9 0 1 7 0 8 7 7
So it can be useful using only on permutations, lists of unique keys, e.g. sorted-then-squeezed lists.

++ 'sort { 1 * ( $a + $b ) }' didn't worked as a reverse with negative numbers, because they influence a final result of sort's sub.

Replies are listed 'Best First'.
Re: RFC: Simple switches for 'sort' and numeric list 'reverse'
by choroba (Archbishop) on Mar 06, 2021 at 23:11 UTC
    > I'm not sure how, but sorting by $a + $b reverses a list.

    Interestingly, it doesn't work under

    use sort '_quicksort';

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
      Good observation!

      But also note that pragma '_quicksort' is undocumented e.g. in Perl 5.32 (5.32.0/sort). The information about default sorting algorithm seems to be absent in documentation*, sadly :(

      Upd. * It is absent in 5.32.0/sort and 5.32.0/functions/sort also, but it is documented in perlsec. I asked about it in an issue and later found that missing info sentence is going to be added into proper place IMO.
Re: RFC: Simple switches for 'sort' and list 'reverse'
by rsFalse (Chaplain) on Mar 12, 2021 at 07:42 UTC
    Updated the original post.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://11129169]
Approved by LanX
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2022-08-12 02:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?