I have a little utility module I use when I get annoyed by the default ASCII sort. Here it is:
package SortCustom; my @d_ord = sort (map{ chr($_) } (0..255)); my %d_map = map { $d_ord[$_] => $_ } (0..$#d_ord); sub csort { my ($A,$B) = @_; my $res = 0; my $ndx = 0; while ($ndx < length($A)) { if (length($B) < $ndx) { $res = 1; last; } my ($cA, $cB) = ( substr($A,$ndx,1) , substr($B,$ndx,1) ); $res = ($d_map{$cA} <=> $d_map{$cB}); last if $res != 0; } continue { $ndx++; } return $res; } sub set_order { my $key = \@_; my @val = sort map { $d_map{$_} } @$key; my ($low, $hi) = ($val[0], $val[-1]); for (@$key) { $d_map{$_} = undef } undef @d_ord; for ( sort { $d_map{$a} <=> $d_map{$b} } keys %d_map ) { next unless defined $d_map{$_}; if ( @d_ord < $low || !defined $key ) { push @d_ord, $_; } else { push @d_ord, @$key; $key = undef; } } %d_map = map { $d_ord[$_] => $_ } (0..$#d_ord); } 1;
This allows you to specify a subset of the default sorting order to "fix" for your own purposes. This particular example (sort + before -) could be "repaired" by:
SortCustom::set_order('+','-'); @set = sort { csort($a,$b) } @set;
The side-effects when making modifcations can be truly odd if you don't think carefully, so take care if using this module in production code. Here's a script that illustrates some of the ways I commonly use this utility:
use strict; use warnings; use List::Util 'shuffle'; use SortCustom; SortCustom::set_order( '-','+', map { $_, lc $_ } ('A'..'Z') ); my @values = shuffle( ('a'..'z','A'..'Z','+','-') ); my @nsort = sort @values; my @csort = sort { SortCustom::csort($a,$b) } @values; for (0..$#values) { printf "%s : %s\n", $nsort[$_], $csort[$_] }
In reply to Re: ASCII Woe
by radiantmatrix
in thread ASCII Woe
by QM
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |