Tuesday, 10 October 2023

Bigdata-Slip30-Write an R program to sort a list of 10 strings in ascending and descending order.

 

Slip 30

Write an R program to sort a list of 10 strings in ascending and descending order.

 

string_list <- c("apple", "banana", "cherry", "date", "elderberry", "fig", "grape", "kiwi", "lemon", "orange")

 

# Sorting in ascending order

sorted_ascending <- sort(string_list)

cat("Sorted in ascending order:\n", sorted_ascending, "\n")

 

# Sorting in descending order

sorted_descending <- sort(string_list, decreasing = TRUE)

cat("Sorted in descending order:\n", sorted_descending, "\n")

 

OutPut

 

> string_list <- c("apple", "banana", "cherry", "date", "elderberry", "fig", "grape", "kiwi", "lemon", "orange")

>

> # Sorting in ascending order

> sorted_ascending <- sort(string_list)

> cat("Sorted in ascending order:\n", sorted_ascending, "\n")

Sorted in ascending order:

 apple banana cherry date elderberry fig grape kiwi lemon orange

>

> # Sorting in descending order

> sorted_descending <- sort(string_list, decreasing = TRUE)

> cat("Sorted in descending order:\n", sorted_descending, "\n")

Sorted in descending order:

 orange lemon kiwi grape fig elderberry date cherry banana apple