# Sorting issue when using locales

**URL:** <https://rubytalk.org/t/sorting-issue-when-using-locales/8262>\
**Category:** ruby-talk\
**Created:** [25 September 2003 21:52 UTC](https://rubytalk.org/t/sorting-issue-when-using-locales/8262 "2003-09-25T21:52:58Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Heinz\_Werntges](https://avatars.discourse-cdn.com/v4/letter/h/a88e4f/32.png) [@Heinz\_Werntges](https://rubytalk.org/u/Heinz_Werntges)\
**Post date:** [25 September 2003 21:52 UTC](https://rubytalk.org/t/sorting-issue-when-using-locales/8262/1 "2003-09-25T21:52:58Z")

</div>

Hi all,

ever used Ruby to sort arrays of strings which contain ISO-8859-1 encoded  
"umlaut" characters as is typical for German text? Recently I found that  
Ruby itself  
does not seem to sort according to my locale setups. Then I learned about  
module “locale”, which seemed to make Ruby do just what I was looking for.

My initial tests using “locale” failed however - Ruby’s sorting order  
does not  
seem to be changed. In contrast, Linux command “sort” reacts as expected  
when changing the locale settings (see attached test protocol.  
Note: using LC\_ALL instead of LC\_COLLATE gave the same results).

Actually I need a solution that (also) works under Windows, but got  
stuck already here.  
Am I trying something stupid? Does the “locale” module perhaps need an  
update,  
as I am running it under Ruby 1.8.0, while it was specified for Ruby 1.6.7?

I tried to contact the author of “locale” a week ago, so far without luck.  
Armin Roehrl kindly provided me with a workaround, but maybe the regular  
approach  
could be fixed, as I expect it to run faster? Any help would be greatly  
appreciated.

Kind regards,

– Heinz

------ Attachment: Test protocol ------------  
heinz@linux:~\> uname  
Linux  
heinz@linux:~\> ruby -v  
ruby 1.8.0 (2003-08-04) [i686-linux]  
heinz@bibo:~\> set | grep LC\_  
LC\_COLLATE=POSIX  
heinz@linux:~\> cat sort\_samples  
a  
z  
ä  
ü  
ö  
ß  
A  
Z  
Ä  
Ö  
Ü  
heinz@linux:~\> od -x sort\_samples  
0000000 0a61 0a7a 0ae4 0afc 0af6 0adf 0a41 0a5a  
0000020 0ac4 0ad6 0adc  
0000026  
heinz@linux:~\> sort \< sort\_samples  
A  
Z  
a  
z  
Ä  
Ö  
Ü  
ß  
ä  
ö  
ü  
heinz@linux:~\> export LC\_COLLATE=de\_DE  
heinz@linux:~\> sort \< sort\_samples  
a  
A  
ä  
Ä  
ö  
Ö  
ß  
ü  
Ü  
z  
Z  
heinz@linux:~\> cat nlsort.rb  
require “locale”

Locale.setlocale(Locale::LC\_COLLATE, ‘de\_DE’)

a = IO.readlines(‘sort\_samples’)  
puts a.sort

heinz@linux:~\> ruby nlsort.rb  
A  
Z  
a  
z  
Ä  
Ö  
Ü  
ß  
ä  
ö  
ü  
#--------- End of protocol -------------
