I want to sort CopyOnWriteArrayList
. But when I tried to run the following code
It is throwing unsorted operation exception
.
Kotlin helper function
inline fun > CopyOnWriteArrayList.sortListBy(crossinline selector: (T) -> R?) {
if (size > 1) {
val list = ArrayList(this)
list.sortBy(selector)
clear()
addAll(list)
}
}
On Android
inline fun > CopyOnWriteArrayList.sortListBy(crossinline selector: (T) -> R?) {
if (size > 1) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
sortBy(selector)
} else {
val list = ArrayList(this)
list.sortBy(selector)
clear()
addAll(list)
}
}
}