如何按本地字符串对字母敏感排序? [英] How to sort by local strings letter sensitive?
本文介绍了如何按本地字符串对字母敏感排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有类似
a, b, c, ą, ć, z, ż
我想按波兰语言环境进行排序,例如:
I want to be sorted with polish locale like:
a, ą, b, c, ć, z, ż
有可能实现吗?
更新:
假设我必须按两个对象参数并使用整理程序对列表进行排序. 对于一个属性,我可以使用:
Suppose I have to sort list by two object parameters and also using collator. For one property I can use:
val collator = Collator.getInstance(context.getResources().getConfiguration().locale)
myList.sortedWith(compareBy(collator, { it.lastName.toLowerCase() }))
如何添加到此也按firstName
排序? (例如,如果会有两个相同的lastName
,然后按firstName
对其进行排序)
How to add to this also to sort by firstName
? (so for example if there will be two same lastName
then sort them by firstName
)
推荐答案
来自尝试这样的事情:
val list = arrayListOf("a", "b", "c", "ą", "ć", "z", "ż")
val coll = Collator.getInstance(Locale("pl","PL"))
coll.strength = Collator.PRIMARY
Collections.sort(list, coll)
println(list)
更新:
使您的对象实现可比性:
Make your object implement Comparable:
override fun compareTo(other: YourObject): Int {
val coll = Collator.getInstance(Locale("pl","PL"))
coll.strength = Collator.PRIMARY
val lastNameCompareValue = coll.compare(lastName?.toLowerCase(Locale("pl","PL")),other.lastName?.toLowerCase(Locale("pl","PL")))
if (lastNameCompareValue != 0) {
return lastNameCompareValue
}
val firstNameCompareValue = coll.compare(firstName?.toLowerCase(Locale("pl","PL")),other.firstName?.toLowerCase(Locale("pl","PL")))
return firstNameCompareValue
}
尝试一下.
这篇关于如何按本地字符串对字母敏感排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文