Any clever/efficient way to sort a list of objects according to another list of ordered values of a specific field?

In other words, I have a list of “id” values:

val ids = listOf(3, 4, 2, 9, 1)

and a list of objects (pseudo code):

val list = listOf(o1: {id: 9}, o2: {id: 2}, o3: {id: 1}, o4: {id: 4}, o5: {id: 3})

and I want to sort the object list such that the order of the id fields corresponds to the first list, like:

val sortedList = listOf(o5, o4, o2, o1, o3)

i.e., { } == ids


Anything cute in the standard library to accomplish this without a lot of fuss?

submitted by /u/ragnese
[link] [comments]