Do we actually need sorting here? What about something like this? fun main() { val things = setOf(Thing(2, "x"), Thing(1, "y"), Thing(3, "z"), Thing(4, "a")) val desiredOrder = listOf(3, 4, 1, 2) val thingsById = things.associateBy { it.id } val sortedThings = desiredOrder.map { thingsById[it] } println(sortedThings) // prints: [Thing(id=3, name=z), Thing(id=4, name=a), Thing(id=1, name=y), Thing(id=2, name=x)] } data class Thing(val id: Int, val name: String)Alternatively, if some of ids in desiredOrder may not be present in things, as in the linked topic, its enough to change map to mapNotNull: fun main() { val things = setOf(Thing(2, "x"), Thing(3, "z"), Thing(4, "a")) val desiredOrder = listOf(3, 4, 1, 2) val thingsById = things.associateBy { it.id } val sortedThings = desiredOrder.mapNotNull { thingsById[it] } println(sortedThings) // prints: [Thing(id=3, name=z), Thing(id=4, name=a), Thing(id=2, name=x)] } data class Thing(val id: Int, val name: String) |