R语言字符串排序

请编写一个函数,能够将一个向量中的字符串按逆序排序。我们通常使用的字符串排序是根
据字符串的首字母从小到大排序,所谓逆序排序是指根据字符串的尾字母从小到大排序,例
如:
c("economics", "employment", "management", "finance")
的逆序排序结果是:
c("finance", "economics", "management", "employment")

str_reverse <- function(x){
  a <- strsplit(x, "")[[1]]
  a <- a[length(a):1]
  return(paste(a, collapse=""))
}

mysort <- function(myvector){
  x <- sapply(myvector, str_reverse)
  return(myvector[order(x)])
}

mysort(c("economics", "employment", "management", "finance"))



 

你可能感兴趣的:(R语言,字符串,快速排序,正则表达式)