1

I'm having issues sorting in Perl having different results in Windows and Unix.

The characters are: a - _ 1 2

In Windows: _ 1 2 - a
In Unix: _ - 1 2 a

I'm guessing the locale has something to do with this - what can I do to make the Unix sort match the Windows sort?

Thanks!

0

2 Answers 2

2

The docs say:

*** WARNING *** The locale specified by the environment affects sort order. Set LC_ALL=C to get the traditional sort order that uses native byte values.

so use

LC_ALL=C sort ...

Example:

$ perl -E'say for @ARGV' a - _ 1 2 | LC_ALL=en_US.UTF-8 sort
_
-
1
2
a

$ perl -E'say for @ARGV' a - _ 1 2 | LC_ALL=C sort
-
1
2
_
a
Sign up to request clarification or add additional context in comments.

4 Comments

These are still not the same as the results in Windows. I need it to match Windows' sort - is there a collation on Unix that will do this?
@Mike, oo, I see that Windows's uses a local too. Well, you'll have to find out what local your Windows uses and see if there's an equivalent one on your unix system, and use it instead of C as I showed. I wish you luck!
That seems to be where I'm at - English_United States.1252 seems to be the Windows one, but I can't yet find a Unix counterpart.
Originally, the following was a comment, but, for more exposure, I just made a new question out of my comment. I then erased the comment, because I don't want to have people answer it twice. I originally addressed comment to @tchrist, because he strikes me as a guru on the matter. stackoverflow.com/questions/15013515/…
0

If you do not want to use locale, comment out the line containing

use locale;

Without such a line, sort in Perl should behave the same on both Windows and Unix.

You can also add

no locale;

before the sort (or, better, enclose the sort into a block starting with it).

4 Comments

I don't have "use locale" in the scripts at all.
@Mike all it takes is one module using it to change it everywhere.
My script is not using any modules at all. I tried no locale anyway with no changes.
@Mike: I'm getting the same results for perl -le 'print sort qw/a - _ 1 2/'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.