jvmMain.io.github.lyxnx.util.collection.Collections.kt Maven / Gradle / Ivy
@file:JvmName("CollectionsJvmKt")
package io.github.lyxnx.util
import java.text.Collator
import java.util.Locale
/**
* Returns all elements sorted by the given [selector] in the order defined by the given [locale], using the [default][Locale.getDefault]
* if null
*
* Eg: `a < ą < b`
*/
@JvmSynthetic
public inline fun Iterable.sortedByLocaleAware(
locale: Locale? = null,
crossinline selector: (T) -> String
): List {
val c = Collator.getInstance(locale ?: Locale.getDefault())
return sortedWith { s1, s2 ->
c.compare(selector(s1), selector(s2))
}
}