sort()
sort(x, decreasing = FALSE, na.last = NA) sort() arranges the elements of a vector in ascending order by default. It preserves the type of the input vector and handles NA values with the na.last parameter.
Syntax
sort(x, decreasing = FALSE, na.last = NA)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
x | vector | , | A vector to sort |
decreasing | logical | FALSE | If TRUE, sorts in descending order |
na.last | logical or NA | NA | Controls placement of NA values |
Examples
Basic usage
# Sort a numeric vector
numbers <- c(5, 2, 8, 1, 9)
sort(numbers)
# [1] 1 2 5 8 9
# Sort in descending order
sort(numbers, decreasing = TRUE)
# [1] 9 8 5 2 1
The default ascending sort works on any atomic vector type — numeric, character, or logical. When decreasing = TRUE is set, sort() flips the comparison direction. This is simpler than manually reversing with rev(sort(x)) and carries no performance overhead beyond the single pass through the data.
Handling NA values
# By default, NA goes to the end
vec <- c(3, NA, 1, 2, NA)
sort(vec)
# [1] 1 2 3 NA NA
# Put NA first
sort(vec, na.last = FALSE)
# [1] NA NA 1 2 3
# Remove NA values
sort(vec, na.last = NA)
# [1] 1 2 3
The na.last argument determines where missing values land in the sorted result — the end, the beginning, or excluded entirely. When na.last = NA (the default), NA values are dropped silently. Setting na.last = TRUE preserves them at the tail, which is useful when the sorted vector must retain the same length as the input for alignment purposes.
Sorting character vectors
names <- c("Charlie", "Alice", "Bob")
sort(names)
# [1] "Alice" "Bob" "Charlie"
Character sorting is lexicographic and locale-dependent — "Alice" comes before "Bob" because "A" < "B" in the current locale’s collation order. For locale-independent string sorting, use sort(x, method = "radix") which sorts by byte value rather than by collation rule and is faster for large character vectors.
Common patterns
Sort a data frame by column
df <- data.frame(name = c("Charlie", "Alice", "Bob"), score = c(85, 92, 78))
df[order(df$score), ]
# name score
# 3 Bob 78
# 1 Charlie 85
# 2 Alice 92
Note that sort() cannot sort data frames directly — it works only on atomic vectors. The pattern df[order(df$col), ] relies on order() to generate a row permutation, which is then applied as a subscript to the data frame. This two-step approach is the base R idiom for ordering tabular data by one or more columns.
Get order indices
# Use order() to get indices instead of sorted values
x <- c("b", "a", "c")
order(x)
# [1] 2 1 3
x[order(x)]
# [1] "a" "b" "c"
sort() in practice
sort() returns a sorted copy of the input vector, it does not modify the original. By default, it sorts in ascending order. sort(x, decreasing = TRUE) sorts descending. For character vectors, sorting is lexicographic and locale-dependent. For named vectors, names are not sorted with the values, they stay attached to their elements and move with them.
For sorting data frames by a column, use df[order(df$col), ] (base R) or dplyr::arrange(df, col). sort() only applies to atomic vectors, not to data frames. sort() with method = "quick" or "shell" changes the underlying sort algorithm; the default since R 3.3 is a fast radix sort for integer and character vectors.
sort() removes NA values by default, they do not appear in the output. To keep NA values in a specific position, use sort(x, na.last = TRUE) or sort(x, na.last = NA) for the default NA-removal behavior. The na.last argument also controls order().
rank() returns the rank of each element (1 = smallest). order() returns the permutation that would sort the vector. sort() returns the sorted values. All three are related: sort(x) equals x[order(x)], and rank(x) gives the inverse permutation of order(x).
sort() returns a new sorted vector without modifying the original. The decreasing argument reverses the order. NA values are removed by default; set na.last = TRUE to sort them to the end, or na.last = FALSE to put them at the front. For data frames, use df[order(df$col), ] or dplyr::arrange(). sort.list() (equivalent to order()) returns the permutation indices rather than sorted values.
sort() returns a new sorted vector without modifying the original. Pass decreasing = TRUE to reverse the order. NA values are excluded by default; set na.last = TRUE to move them to the end, or na.last = FALSE to put them at the front.
sort.default() uses a shell sort for short vectors and a quicksort for longer ones, both are O(n log n) on average. For character vectors, sorting is locale-dependent; use sort(x, method = "radix") for deterministic locale-independent sorting, which is also faster for large character vectors.
For data frames, df[order(df$col), ] sorts by a column in base R; dplyr::arrange() is cleaner in a pipeline. sort() is not for data frames directly, it coerces them, dropping structure.
sort.list() is an alias for order() — it returns integer positions rather than sorted values. Use it when you need both the sorted result and a way to recover original positions: idx <- order(x); x[idx] is the sorted vector, and idx lets you apply the same permutation to a parallel vector.