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!

2 Answers2

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
ikegami
  • 367,544
  • 15
  • 269
  • 518
  • 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? – Michael Sweetser Feb 16 '12 at 16:58
  • @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! – ikegami Feb 16 '12 at 18:32
  • 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. – Michael Sweetser Feb 16 '12 at 19:29
  • 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. http://stackoverflow.com/questions/15013515/multilingual-text-sorting-in-perl-on-windows-using-locale – egilchri Feb 21 '13 at 22:34
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).

choroba
  • 231,213
  • 25
  • 204
  • 289