Skip to contents

This function extends the sorting capabilities for alphanumeric strings by allowing for sorting of mixed numeric and character strings, with additional control over sorting direction, treatment of NA and blank values, and handling of numeric values represented as either decimal numbers or Roman numerals. This function is a wrapper function for the gtools::mixedsort function.

Usage

sort_(
  x,
  decreasing = FALSE,
  na.last = TRUE,
  blank.last = FALSE,
  numeric.type = c("decimal", "roman"),
  roman.case = c("upper", "lower", "both")
)

Arguments

x

Vector to be sorted.

decreasing

logical. Should the sort be increasing or decreasing? Note that descending=TRUE reverses the meanings of na.last and blanks.last.

na.last

for controlling the treatment of NA values. If TRUE, missing values in the data are put last; if FALSE, they are put first; if NA, they are removed.

blank.last

for controlling the treatment of blank values. If TRUE, blank values in the data are put last; if FALSE, they are put first; if NA, they are removed.

numeric.type

either "decimal" (default) or "roman". Are numeric values represented as decimal numbers (numeric.type="decimal") or as Roman numerals (numeric.type="roman")?

roman.case

one of "upper", "lower", or "both". Are roman numerals represented using only capital letters ('IX') or lower-case letters ('ix') or both?

Value

A vector of sorted alphanumeric strings.

Examples

# example code
(AA <- paste0("V", seq_len(12)))
#>  [1] "V1"  "V2"  "V3"  "V4"  "V5"  "V6"  "V7"  "V8"  "V9"  "V10" "V11" "V12"

sort(x = AA)
#>  [1] "V1"  "V10" "V11" "V12" "V2"  "V3"  "V4"  "V5"  "V6"  "V7"  "V8"  "V9" 

sort_(x = AA)
#>  [1] "V1"  "V2"  "V3"  "V4"  "V5"  "V6"  "V7"  "V8"  "V9"  "V10" "V11" "V12"