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[$_] }
<-radiant.matrix->
A collection of thoughts and links from the minds of geeks
The Code that can be seen is not the true Code
I haven't found a problem yet that can't be solved by a well-placed trebuchet

In reply to Re: ASCII Woe by radiantmatrix
in thread ASCII Woe by QM

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.