Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Kotlin sorting by collator

I want to sort a list of objects based on one field(player.name), but in Spanish there are accents that don't have to be taken into account when ordering.

I sort the list:

strikers.sortedWith(compareBy { it.name })

But I have no idea how to apply to the above sorting

val spanishCollator = Collator.getInstance(Locale("es", "ES"))

How can I achieve this?

like image 447
niegus Avatar asked Dec 21 '18 09:12

niegus


2 Answers

Something like this?

val spanishCollator = strikers.sortedWith(Comparator { s1, s2 ->
                Collator.getInstance(Locale("es", "ES")).compare(s1,s2)
            })
like image 64
nhp Avatar answered Nov 01 '22 00:11

nhp


Collator class implements Comparator interface, so you can use it to compare names as following:

strikers.sortedWith(compareBy(spanishCollator) { it.name })

Here we use it as a comparator argument of compareBy function overload, that takes both the value selector { it.name } and the comparator spanishCollator that compares these values.

like image 41
Ilya Avatar answered Nov 01 '22 00:11

Ilya