Order or sort a character vector.

str_order(x, decreasing = FALSE, na_last = TRUE, locale = "en",
  numeric = FALSE, ...)

str_sort(x, decreasing = FALSE, na_last = TRUE, locale = "en",
  numeric = FALSE, ...)

Arguments

x

A character vector to sort.

decreasing

A boolean. If FALSE, the default, sorts from lowest to highest; if TRUE sorts from highest to lowest.

na_last

Where should NA go? TRUE at the end, FALSE at the beginning, NA dropped.

locale

In which locale should the sorting occur? Defaults to the English. This ensures that code behaves the same way across platforms.

numeric

If TRUE, will sort digits numerically, instead of as strings.

...

Other options used to control sorting order. Passed on to stringi::stri_opts_collator().

See also

stringi::stri_order() for the underlying implementation.

Examples

str_order(letters)
#> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #> [26] 26
str_sort(letters)
#> [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" #> [20] "t" "u" "v" "w" "x" "y" "z"
str_order(letters, locale = "haw")
#> [1] 1 5 9 15 21 2 3 4 6 7 8 10 11 12 13 14 16 17 18 19 20 22 23 24 25 #> [26] 26
str_sort(letters, locale = "haw")
#> [1] "a" "e" "i" "o" "u" "b" "c" "d" "f" "g" "h" "j" "k" "l" "m" "n" "p" "q" "r" #> [20] "s" "t" "v" "w" "x" "y" "z"
x <- c("100a10", "100a5", "2b", "2a") str_sort(x)
#> [1] "100a10" "100a5" "2a" "2b"
str_sort(x, numeric = TRUE)
#> [1] "2a" "2b" "100a5" "100a10"