To sort a list in ascending order in R, you can use the sort()
function.
In this example,
numbers
with integer values.sort()
function on the numbers
list to sort it in ascending order.sort()
function returns a new list with the elements sorted.numbers <- c(5, 2, 8, 1, 3)
sorted_numbers <- sort(numbers)
cat("Sorted list in ascending order:", sorted_numbers)
Sorted list in ascending order: 1 2 3 5 8
In this example,
words
with string values.sort()
function on the words
list to sort it in ascending order.sort()
function returns a new list with the elements sorted.words <- c("banana", "apple", "cherry")
sorted_words <- sort(words)
cat("Sorted list in ascending order:", sorted_words)
Sorted list in ascending order: apple banana cherry
In this tutorial, we learned How to Sort a List in Ascending Order in R language with well detailed examples.