Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Sorting (GRT) and locale issues

by ikegami (Patriarch)
on Nov 15, 2007 at 04:46 UTC ( [id://650918]=note: print w/replies, xml ) Need Help??


in reply to Sorting (GRT) and locale issues

It seems to me that regardless of locale/no locale, it should sort nums before letters, as per BrowserUK's tutorial.

What numbers? You don't provide any numbers to sort. You first convert the numbers into strings of 4 characters. The resulting characters could be anything, including letters. sort is doing a lexical sort on them as requested, and applies locale rules as requested even though it makes no sense to do so.

In this specific case, locale could very well affect how "µ" and "Ù" sort.

my $NUL     = chr(0x00);
my $SOH     = chr(0x01);
my $STX     = chr(0x02);
my $undef93 = chr(0x93);

001: "${NUL}${NUL}${NUL}${SOH}"
123: "${NUL}${NUL}${NUL}{"
222: "${NUL}${NUL}${NUL}Þ"
437: "${NUL}${NUL}${SOH}µ"
473: "${NUL}${NUL}${SOH}Ù"
659: "${NUL}${NUL}${STX}${undef93}"

Since you want part of the string sorted numerically and part of the string sorted lexically (using the locale), you'll have to limit yourself to the ST.

@arr = map{ $_->[0] } sort { $a->[1] <=> $b->[1] || $a->[2] cmp $b->[2] } map{ [ $_, substr( $_, 1 ), substr( $_, 0, 1 ) ] } qw[ A473 B437 B659 C659 C123 D123 D222 E222 E001 A001 ];

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-24 01:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found