Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Kotlin 如何按字母敏感的本地字符串排序?_Kotlin_Functional Programming - Fatal编程技术网

Kotlin 如何按字母敏感的本地字符串排序?

Kotlin 如何按字母敏感的本地字符串排序?,kotlin,functional-programming,Kotlin,Functional Programming,假设我有一个单词列表,比如 a, b, c, ą, ć, z, ż 我希望按照波兰语言环境进行排序,如: a, ą, b, c, ć, z, ż 有可能实现吗 更新: 假设我必须按两个对象参数对列表进行排序,并使用collator。 对于一个属性,我可以使用: val collator = Collator.getInstance(context.getResources().getConfiguration().locale) myList.sortedWith(compareBy(co

假设我有一个单词列表,比如

a, b, c, ą, ć, z, ż
我希望按照波兰语言环境进行排序,如:

a, ą, b, c, ć, z, ż
有可能实现吗

更新:

假设我必须按两个对象参数对列表进行排序,并使用collator。 对于一个属性,我可以使用:

val collator = Collator.getInstance(context.getResources().getConfiguration().locale)

myList.sortedWith(compareBy(collator,  { it.lastName.toLowerCase() }))
如何将其添加到按
firstName
排序中?(例如,如果有两个相同的
lastName
,则按
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)
更新:

使您的对象实现具有可比性:

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
}

试试这个。

谢谢,你能看到我更新的问题吗?也许你知道如何结合你给我的答案previously@Konrad您应该在对象中实现comparable接口,并在compareTo方法中执行该逻辑,但仍然不确定这对我有多大帮助-我仍然无法对它们应用按两个属性排序和collator这是一个好的解决方案。唯一的问题是当我需要另一个项圈时怎么办?通常情况下,它是由上下文提供的,但在某些具有这种可比性的数据类对象中很难有上下文implemented@Konrad您可以通过创建一个应用程序类(如果没有)并从中获取实例来获取上下文,因此可以执行类似于
val context=MyAppClass.getInstance()