Do not ignore case in sorting character strings

后端 未结 1 1127
我寻月下人不归
我寻月下人不归 2020-12-11 03:04

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

tv <-          


        
相关标签:
1条回答
  • 2020-12-11 03:45

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

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

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

    To temporally change collate for sorting you could use withr package:

    withr::with_collate("C", sort(tv))
    

    or use stringr package (as in @dracodoc comment):

    stringr::str_sort(tv, locale="C")
    

    I think this is the best way to do it.

    0 讨论(0)
提交回复
热议问题