order() and sort()

  • order() returns a vector that lists indices corresponding to the values of ‘x’
x <- c(10, 20, 30, 50, 40)
order(x)
## [1] 1 2 3 5 4
  • sort() returns the original vector, in ascending order
sort(x)
## [1] 10 20 30 40 50
  • sort(x) is equivalent to x[order(x)]
all.equal(sort(x), x[order(x)])
## [1] TRUE