R Programming: Sort a Vector in ascending and descending order

R Programming: Vector Exercise-9 with Solution

Write a R program to sort a Vector in ascending and descending order.

Sample Solution :

R Programming Code :

x = c(10, 20, 30, 25, 9, 26)
print("Original Vectors:")
print("Sort in ascending order:")
print("Sort in descending order:")
print(sort(x, decreasing=TRUE))

Sample Output:

[1] "Original Vectors:"
[1] 10 20 30 25  9 26
[1] "Sort in ascending order:"
[1]  9 10 20 25 26 30
[1] "Sort in descending order:"
[1] 30 26 25 20 10  9                         

R Programming Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a R program to find the minimum and the maximum of a Vector.
Next: Write a R program to test whether a given vector contains a specified element.

Test your Programming skills with w3resource's quiz.

What is the difficulty level of this exercise?

Follow us on Facebook and Twitter for latest update.