Skip to content
Prev 374751 / 398513 Next

find the permutation function of a sorting

Hello,

Like David said, what you are trying to do with sort() can be done with 
order() in a much easier way.

First, your code

x <- sort(c("bc","ac","dd"), index.return=TRUE)


Now, with function order()


i <- order(c("bc", "ac", "dd"))

y <- c("D","E", "F")[i]
y
#[1] "E" "D" "F"

# This will give you the inverse,
# just apply order() to the output of order(),
# function order() is its own inverse

y[ order(i) ]
#[1] "D" "E" "F"


Finally, compare the results and see that they are exactly the same.

identical(x$ix, i)
#[1] TRUE


Hope this helps,

Rui Barradas
On 5/23/2018 4:37 AM, John wrote: