gpt4 book ai didi

ruby - 如何将 UTF-8 支持添加到 Ruby 中的排序(包括 ł 字符,而不影响可移植性)?

转载 作者:数据小太阳 更新时间:2023-10-29 08:06:59 25 4
gpt4 key购买 nike

Ruby 字符串函数不支持 UTF-8。

例如 ['l', 'ł', 'm'].sort 返回 ["l", "m", "ł"] 而不是["l", "ł", "m"]

应该如何在 Ruby 中对 UTF-8 字符串进行排序?

Sorting UTF-8 strings in RoR - 接受的答案不支持 ł 字符( issue open since 2015 ,被 PR waiting opened in 2014 阻止),截至 2017 年 10 月 8 日未合并。

ffi-icu answer适用于安装了 libicu 的系统,AFAIK 不是真正可移植的。

最佳答案

好的解决方案是使用 gem https://github.com/twitter/twitter-cldr-rb

require 'twitter_cldr'
collator = TwitterCldr::Collation::Collator.new
collator.sort(['m', 'ł', 'l'])
=> ["l", "ł", "m"]

关于ruby - 如何将 UTF-8 支持添加到 Ruby 中的排序(包括 ł 字符,而不影响可移植性)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44149310/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com