Created
May 13, 2010 20:34
-
-
Save pmarreck/400426 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class String | |
# note: very rough, does not handle caps (but that could be added), just something to help sort accented characters correctly... | |
TRANSLATION_HASH = {"’"=>"'","åäáàâ" => "a", "ëéèê" => "e", "ïíìî" => "i", "öóòô" => "o", "üúùû" => "u", "ß" => "ss"} | |
UNUNICODE = Hash[*TRANSLATION_HASH.map{|u,as| u.unpack("U*").inject([]){|sum,e| sum << e; sum << as}}.flatten] | |
def un_unicode | |
self.unpack("U*").map{|c| UNUNICODE[c] ? UNUNICODE[c] : c.chr}.join | |
end | |
end | |
# use case | |
# array_of_strings_with_some_unicode.sort_by{|s| s.un_unicode } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Or you can use ActiveSupport::Inflector.transliterate
http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-transliterate