Sort Multiple Columns Alphabetically

We can also sort multiple columns in dataframe by using order function.

Syntax:

dataframe[with(dataframe, order(column1, column2,.,column n)), ]

Example:

R




# create a dataframe with 3 columns
data = data.frame(name1=c('G', 'E', 'E', 'K', 'S'), 
                  name2=c('P', 'Y', 'T', 'H', 'O'), 
                  marks=c(78, 89, 77, 89, 78))
  
# sort the dataframe based on name1 and 
# name2 columns
print(data[with(data, order(name1, name2)), ])


Output:



How to Sort Values Alphabetically in R?

In this article, we will discuss how to sort values alphabetically in R Programming Language.

Similar Reads

Sorting vector Alphabetically

Here we are using sort() function to sort a vector alphabetically....

Sorting Data Frame Column Alphabetically

...

Method 3: Sort Multiple Columns Alphabetically

We can create a dataframe by using date.frame() function. We can sort a dataframe column by using order() function...