Sorting UTF-8 strings in RoR

前端 未结 7 913
暗喜
暗喜 2020-12-09 18:12

I am trying to figure out a \'proper\' way of sorting UTF-8 strings in Ruby on Rails.

In my application, I have a select box that is populated with countries. As my

7条回答
  •  甜味超标
    2020-12-09 18:21

    The only solution I have found thus far is to use ActiveSupport::Inflector.transliterate(string) to replace the unicode characters with ASCII ones and sort:

    Country.all.sort_by do |country|
      ActiveSupport::Inflector.transliterate country.name
    end
    

    Now the only problem is that this equalizes "ä" with "a" (DIN 5007-1) and I end up with "Ägypten" before "Albanien" while I would expect it to be the other way around. Thankfully the transliteration is configurable about how to replace characters.

    See documentation: http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-transliterate

提交回复
热议问题