在排序字符串时不要忽略大小写

在排序字符串时不要忽略大小写

本文介绍了在排序字符串时不要忽略大小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在R中有一个内置函数来排序字符向量考虑案例? sort order 忽略大小写:

Is there a builtin functionality in R to sort character vectors taking case into account? sort and order ignore the case:

tv <- c("a", "A", "ab", "B")
sort(tv)
## [1] "a"  "A"  "ab" "B"

这是我的解决方案:

CAPS <- grep("^[A-Z]", tv)
c(sort(tv[CAPS]), sort(tv[-CAPS]))
## [1] "A"  "B"  "a"  "ab"


推荐答案

关注您可以更改本地设置:

Following post about Auto-completion in Notepad++ you could change local settings:

Sys.setlocale(, "C")
sort(tv)
# [1] "A"  "B"  "a"  "ab"






编辑。我阅读帮助页面 Sys.setlocale ,似乎更改 LC_COLLATE 是足够的: Sys .setlocale(LC_COLLATE,C)


EDIT. I read help pages to Sys.setlocale and it seems that changing LC_COLLATE is sufficient: Sys.setlocale("LC_COLLATE", "C")

如果你多次使用它, p>

You could wrap it into a function if you use it more than once:

sortC <- function(...) {
    a <- Sys.getlocale("LC_COLLATE")
    on.exit(Sys.setlocale("LC_COLLATE", a))
    Sys.setlocale("LC_COLLATE", "C")
    sort(...)
}

这篇关于在排序字符串时不要忽略大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 17:28