Warm tip: This article is reproduced from stackoverflow.com, please click
kotlin sorting

Kotlin sorting list without 1 element

发布于 2020-04-04 10:11:09

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

Questioner
CallMePedro
Viewed
48
Egor Zaitsev 2020-01-31 21:45
list = list.sortedWith(Comparator { a, b -> when {
    a.id == 3 -> -1
    b.id == 3 -> 1
    else -> Integer.compare(a.id, b.id)
}})