To sort a Vector in R programming, we are using a sort() function. The sort() function is an R built-in function that can able to arrange the vectors in both ascending and descending order. by default the sort() function will arrange in ascending order, if we need it in descending order we have set decreasing=TRUE. The syntax is like
sort(x, decreasing, na.last)
In this R program, we directly give the values to the built-in function sort(). For that first, we have to add the vector values into 'A'. Call the sort() function twice to arrange the vectors in ascending order, and in descending order by setting decreasing=TRUE in the second function call. Finally, print the result.
STEP 1: Assign variable A with vector values
STEP 2: display the original values of the vector first
STEP 3: Call the sort() for getting results in ascending order
STEP 4: Print the result
STEP 5: Call sort() function again by setting decreasing=TRUE
STEP 6: Print the result of the function
A= c(10, 12, 30, 25, 8, 29)
print("Original Vectors:")
print(A)
print("Sort in ascending order:")
print(sort(A))
print("Sort in descending order:")
print(sort(A, decreasing=TRUE))
[1] "Original Vectors:" [1] 10, 12, 30, 25, 8, 29 [1] "Sort in ascending order:" [1] 8 10 12 25 29 30 [1] "Sort in descending order:" [1] 30 29 25 12 10 8