hannaminx
hannaminx

Reputation: 63

How to sort an array of accented words in ruby

How can I sort an array of accented words by the every letter in reference to variable alpha. The code below only reference alpha for the first letter so I am unable to get "ĝusti", "ĝusti vin","ĝuspa" to sort correctly.

I need the code to sort the words like this:

["bonan matenon", "ĉu vi parolas esperanton","ĝuspa", "ĝusti", "ĝusti vin",  "mi amas vin", "pacon"]

def alphabetize(phrases)
    alpha = "abcĉdefgĝhĥijĵklmnoprsŝtuŭvz".split(//)

    phrases.sort_by { |phrase|      
    alpha.index(phrase[0])  
    }
    end

alphabetize(["mi amas vin", "bonan matenon", "pacon", "ĉu vi parolas esperanton", "ĝusti", "ĝusti vin","ĝuspa"])

Upvotes: 6

Views: 1140

Answers (2)

peter
peter

Reputation: 42207

You could use the i18n gem like this:

# encoding: UTF-8
require 'i18n'
I18n.enforce_available_locales = false

a = ["bonan matenon", "ĉu vi parolas esperanton","ĝuspa", "ĝusti", "ĝusti vin",  "mi amas vin", "pacon"]
b = a.sort_by { |e| I18n.transliterate e }
puts b

gives

bonan matenon
ĉu vi parolas esperanton
ĝuspa
ĝusti
ĝusti vin
mi amas vin
pacon

Upvotes: 14

Stefan
Stefan

Reputation: 114248

The fix is quite obvious: instead of just returning the first character's index, map all characters to their respective index:

def alphabetize(phrases)
  alpha = "abcĉdefgĝhĥijĵklmnoprsŝtuŭvz".chars

  phrases.sort_by do |phrase|
    phrase.chars.map { |c| alpha.index(c) }
  end
end

puts alphabetize(["mi amas vin", "bonan matenon", "pacon", "ĉu vi parolas esperanton", "ĝusti", "ĝusti vin","ĝuspa"])

Output:

bonan matenon
ĉu vi parolas esperanton
ĝuspa
ĝusti
ĝusti vin
mi amas vin
pacon

To speed up index lookup, you could use a hash:

alpha = "abcĉdefgĝhĥijĵklmnoprsŝtuŭvz".each_char.with_index.to_h
#=> {"a"=>0, "b"=>1, "c"=>2, ..., "v"=>26, "z"=>27}

and call alpha[c] instead of alpha.index(c)

Upvotes: 7

Related Questions