3

The following code found that it was not be able to sort an array.

For some reasons, the last item "Aland Islands" was located in the last for this array, but it should be located in between "Afghanistan" and "Albania". And this occurs just in case of using iso3166 like below, but does not occur in scratching. Any ideas?

$ which ruby
~/.rvm/rubies/ruby-1.9.3-p362/bin/ruby

$ cat test.rb
#!/usr/bin/env ruby
# coding: utf-8

require 'iso3166'

countries = ISO3166::Country::all.map do |c|
  c.first
end

p countries.sort

$ ruby test.rb
["Afghanistan", "Albania", ... etc ... "Zambia", "Zimbabwe", "Aland Islands"]
Dave Newton
  • 158,873
  • 26
  • 254
  • 302
diveintohacking
  • 4,783
  • 6
  • 29
  • 43
  • 2
    What is the output of `.codepoints.to_a` when called on that last element ? Call this method on the resultant array of the sort - **NOT by typing** in "Aland Islands" by hand. See if this article helps: http://www.rubyinside.com/the-split-is-not-enough-whitespace-shenigans-for-rubyists-5980.html That string you have may not be in ASCII.. – Zabba Jan 05 '13 at 22:29
  • 4
    Got it: The string is "ÅLAND ISLANDS", that is why. And for me, `c.first` throws an error. – Zabba Jan 05 '13 at 22:34
  • 1
    `'A'.ord => 65` versus `'Z'[0].ord => 90` versus `'Åland Islands'[0].ord => 197` – the Tin Man Jan 05 '13 at 22:43
  • 'Åland Islands'[0].ord => 197, this is the reason so the order by the code above is all correct, I got it! I appreciate a quick response. – diveintohacking Jan 05 '13 at 23:51

1 Answers1

4

Here's my comment as an answer:

'A'.ord => 65 

versus

'Z'[0].ord => 90

versus

'Åland Islands'[0].ord => 197
the Tin Man
  • 158,662
  • 42
  • 215
  • 303