-
Notifications
You must be signed in to change notification settings - Fork 193
/
Copy pathsort.R
95 lines (87 loc) · 2.6 KB
/
sort.R
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#' Order, rank, or sort a character vector
#'
#' * `str_sort()` returns the sorted vector.
#' * `str_order()` returns an integer vector that returns the desired
#' order when used for subsetting, i.e. `x[str_order(x)]` is the same
#' as `str_sort()`
#' * `str_rank()` returns the ranks of the values, i.e.
#' `arrange(df, str_rank(x))` is the same as `str_sort(df$x)`.
#'
#' @param x A character vector to sort.
#' @param decreasing A boolean. If `FALSE`, the default, sorts from
#' lowest to highest; if `TRUE` sorts from highest to lowest.
#' @param na_last Where should `NA` go? `TRUE` at the end,
#' `FALSE` at the beginning, `NA` dropped.
#' @param numeric If `TRUE`, will sort digits numerically, instead
#' of as strings.
#' @param ... Other options used to control collation. Passed on to
#' [stringi::stri_opts_collator()].
#' @inheritParams coll
#' @return A character vector the same length as `string`.
#' @seealso [stringi::stri_order()] for the underlying implementation.
#' @export
#' @examples
#' x <- c("apple", "car", "happy", "char")
#' str_sort(x)
#'
#' str_order(x)
#' x[str_order(x)]
#'
#' str_rank(x)
#'
#' # In Czech, ch is a digraph that sorts after h
#' str_sort(x, locale = "cs")
#'
#' # Use numeric = TRUE to sort numbers in strings
#' x <- c("100a10", "100a5", "2b", "2a")
#' str_sort(x)
#' str_sort(x, numeric = TRUE)
str_order <- function(x,
decreasing = FALSE,
na_last = TRUE,
locale = "en",
numeric = FALSE,
...) {
check_bool(decreasing)
check_bool(na_last, allow_na = TRUE)
check_string(locale)
check_bool(numeric)
opts <- stri_opts_collator(locale, numeric = numeric, ...)
stri_order(x,
decreasing = decreasing,
na_last = na_last,
opts_collator = opts
)
}
#' @export
#' @rdname str_order
str_rank <- function(x,
locale = "en",
numeric = FALSE,
...) {
check_string(locale)
check_bool(numeric)
opts <- stri_opts_collator(locale, numeric = numeric, ...)
stri_rank(x,
opts_collator = opts
)
}
#' @export
#' @rdname str_order
str_sort <- function(x,
decreasing = FALSE,
na_last = TRUE,
locale = "en",
numeric = FALSE,
...) {
check_bool(decreasing)
check_bool(na_last, allow_na = TRUE)
check_string(locale)
check_bool(numeric)
opts <- stri_opts_collator(locale, numeric = numeric, ...)
stri_sort(x,
decreasing = decreasing,
na_last = na_last,
opts_collator = opts
)
}