Build a permutation p such that ids1[p] == ids2. See https://win-vector.com/2017/09/02/permutation-theory-in-action/.

match_order(ids1, ids2)

Arguments

ids1

unique vector of ids.

ids2

unique vector of ids with sort(ids1)==sort(ids2).

Value

p integers such that ids1[p] == ids2

Examples

ids1 <- c(4, 5, 7, 8, 9, 6, 1, 3, 2, 10) ids2 <- c(3, 6, 4, 8, 5, 7, 1, 9,10, 2) p <- match_order(ids1, ids2) ids1[p]
#> [1] 3 6 4 8 5 7 1 9 10 2
all.equal(ids1[p], ids2)
#> [1] TRUE
# note base::match(ids2, ids1) also solves this problem