VOOZH about

URL: https://www.geeksforgeeks.org/r-language/sorting-of-a-vector-in-r-programming-sort-function/

⇱ Sorting of a Vector in R Programming - sort() Function - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Sorting of a Vector in R Programming - sort() Function

Last Updated : 15 Jul, 2025

In R Programming Language we can sort a vector in ascending or descending order using the sort() function. The sort() function returns a sorted version of the input vector.

sort() function in is used to sort a vector by its values. It takes the Boolean value as an argument to sort in ascending or descending order.

Syntax:

sort(x, decreasing, na.last)

Parameters:x: Vector to be sorted

  • decreasing: Boolean value to sort in descending order
  • na.last: Boolean value to put NA at the end

R program to sort a vector

Example 1:

Output:

[1] -8.0 -5.0 -4.0 1.2 3.0 4.0 6.0 7.0 9.0

Example 2:

Output:

[1] 9.0 7.0 6.0 4.0 3.0 1.2 -4.0 -5.0 -8.0
[1] -8.0 -5.0 -4.0 1.2 3.0 4.0 6.0 7.0 9.0 NA

Example 3:

Output:

[1] "Jhala" "Raj" "Singh" "Vipul"
Comment
Article Tags:

Explore