Search code examples
androidkotlincomparable

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)

Solution

  • 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 })