Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

In essence, all you need is a custom string comparison routine. And the easiest, most efficient way to do that in Perl is to use the tr/// operator to 'encode' the strings and then use the standard cmp operator.

Say your custom sort rules call for 0-4 to be sorted before alphas, and 5-9 after. And within the alphas, you want upper and lower case of any given character to be sorted together. Then you set up a mapping that maps the original strings to characters that will sort in the required order.

For the given example:

tr[0-4AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz5-9][\x00-\x +ff];
will do the trick. Now, you just transliterate your string and sort in the normal way:
#! perl -slw use strict; sub trans { my $in = shift; $in =~ tr[0-4AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz5 +-9] [\x00-\xff]; return $in; } chomp( my @data = <DATA> ); my @sorted = sort{ trans( $a ) cmp trans( $b ) } @data; print for @sorted; __DATA__ cdef 0123456 abcd 50011 ABCD 4999 Zxyw CDEF zxyw 9999

Produces:

c:\test>junk78 0123456 4999 ABCD abcd CDEF cdef Zxyw zxyw 50011 9999

Of course, you can now apply all the usual forms of sort optimisations--ST, GRT etc.--to that, but the mechanism remains the same.

The ugly head of UTF will probably complicate things a little, but, at least in the eyes of my non-UTF aware brain, it should be possible to apply the same mechanism.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re: New Alphabet Sort Order by BrowserUk
in thread New Alphabet Sort Order by Polyglot

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chanting in the Monastery: (6)
    As of 2014-08-01 09:58 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Who would be the most fun to work for?















      Results (0 votes), past polls