开发者

Rails find and sort using natural sort order collation

开发者 https://www.devze.com 2023-01-16 20:46 出处:网络
I want to return a paginated list of countries with 开发者_运维问答Åland coming after Azerbaijan instead of after Zimbabwe. In other words, I want to ignore special characters and simply treat an \"Å

I want to return a paginated list of countries with 开发者_运维问答Åland coming after Azerbaijan instead of after Zimbabwe. In other words, I want to ignore special characters and simply treat an "Å" as an "A" and the "ô" in Côte d'Ivoire as a regular "o". Is there a Rails method or gem to do this or do I need to execute some kind of custom SQL (and if so, what kind)?


Look at How do I replace accented Latin characters in Ruby?.

You should be able to sort the countries by their normalized names.

Something like:

@countries.sort{|x,y| x.name.chars.normalize(:kd).gsub(/[^\x00-\x7F]/n,'').downcase.to_s <=> y.name.chars.normalize(:kd).gsub(/[^\x00-\x7F]/n,'').downcase.to_s}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号