scala - Sort a list given a list of indices -
say have 1 unordered list
val unsorted = list("third", "second", "fourth", "first")
and have list has indices of above list in proper order
val ids = list(3, 1, 0, 2)
how can sort unsorted
using these indices result
list("first", "second", "third", "fourth")
simply map ids onto unsorted list itself.
scala> val sorted = ids map unsorted.toindexedseq sorted: list[string] = list(first, second, third, fourth)
converting unsorted
indexedseq
not necessary, @gzm0 points out below prevents operation being o(n^2)
.
Comments
Post a Comment