Guilherme Lima Pereira
Guilherme Lima Pereira

Reputation: 1434

How to change the order of compareBy in Kotlin

I need a comparator, in Kotlin, to order every object that enter in my recycler view (using Fast Adapter).

I need to order the objects by an Integer, where the biggest ones come first.

The code above order the object that enters in my list, but the biggest ones is in the end of the list. Is there a way to reverse the order?

playersFastAdapter.itemAdapter.withComparator(compareBy({ it.player.goals }, {it.player.assists}), true)

Upvotes: 5

Views: 4650

Answers (3)

peter
peter

Reputation: 96

mylist.sortWith(compareBy<Player> { it.name }.thenBy { it.age }.thenBy { it.gender })

or

mylist.sortWith(compareByDescending<Player> { it.name }
    .thenByDescending { it.age }.thenBy { it.gender })

Upvotes: 8

Guilherme Lima Pereira
Guilherme Lima Pereira

Reputation: 1434

As suggested by Mitja Slenc on Kotlin forum:

Changed to compareBy({ -it.player.goals}, {-it.player.assists})

And now it's working the way I wanted!

Upvotes: 3

James Ralston
James Ralston

Reputation: 1208

you can always use compareByDescending

Upvotes: 0

Related Questions