In this case I would probably build a map from this "master list", from each ID to its position in the order, and use that to write a custom sort function like this:
/** Defines the total order between all IDs */
private val order = listOf(3, 4, 2, 1, 5)
/** Maps each ID to its rank in the total order */
private val rankById = order.withIndex().associate { (index, id) -> id to index }
private fun List<Int>.sortedByCustomOrder() = sortedBy { rankById[it] }
fun main() {
println(listOf(1, 2, 3, 4, 5).sortedByCustomOrder()) // [3, 4, 2, 1, 5]
println(listOf(1, 2, 3).sortedByCustomOrder()) // [3, 2, 1]
println(listOf(2, 4, 5).sortedByCustomOrder()) // [4, 2, 5]
}