使用PyICU,如何使用排序器按照“自然顺序”对字符串列表进行排序,即将10放在2之后而不是之前?
在ICU文档http://userguide.icu-project.org/collation/customization#TOC-Default-Options中,我可以看到有一个"numericOrdering“选项(a.k.a )。可以设置为on或off,但我不知道如何从UCOL_NUMERIC_COLLATION代码中设置该属性。
发布于 2020-06-02 08:08:00
可以在排序规则实例上使用.setAttribute方法。
属性名称和值来自附加到主icu模块的枚举:
import icu
collator = icu.Collator.createInstance(icu.Locale('en_US'))
collator.setAttribute(icu.UCollAttribute.NUMERIC_COLLATION, icu.UCollAttributeValue.ON)
sorted(['3 three', '1 one', '10 ten', '2 two'])
# ['1 one', '10 ten', '2 two', '3 three']
sorted(['3 three', '1 one', '10 ten', '2 two'], key=collator.getSortKey)
# ['1 one', '2 two', '3 three', '10 ten']https://stackoverflow.com/questions/62147537
复制相似问题