I have a list of objects and I would like to compare them by ids field. However, I would like to item with id=3
to show first, and then id=0, id=1
and so on.
list = list.sortedWith(compareBy<MyItem> {it.id})
I was trying a lot of combinations but don't know where to add if statement.
Greetings
list = list.sortedWith(Comparator { a, b -> when {
a.id == 3 -> -1
b.id == 3 -> 1
else -> Integer.compare(a.id, b.id)
}})
What if both
a
andb
are 3?The else could read:
else -> a.id.compareTo(b.id)