Reputation: 33
Say I have a vector and its name as follows:
vct <- c(67, "apple", 88, "orange", 71)
names(vct) <- c("c1", "b2", "d66", "a65", "a11")
when I run:
sort(vct)
the vector is sorted based on elements; rather I want it to be sorted based on its names so that the vector is ordered "71", "orange", "apple", "67", "88".
Thanks in advance!
Upvotes: 1
Views: 298
Reputation: 388982
You can sort
or order
the names :
vct[sort(names(vct))]
#vct[order(names(vct))]
# a11 a65 b2 c1 d66
# "71" "orange" "apple" "67" "88"
Upvotes: 2