# sort, order & rank R Functions | 6 Examples: Data Table, List & by Group Column

This article explains how to **sort data in R** with the functions sort(), order(), and rank().

The tutorial shows in **six examples** how the different sorting functions can be applied in the R programming language.

So without further ado, let’s move on to the examples!

## Example 1: sort vs. order vs. rank (Basic Application)

Let’s first create an example vector in R, which we can use in the following examples:

x <- c(4, - 10, 8, 0) # Create example vector x # Print vector to console # 4 -10 8 0 |

x <- c(4, - 10, 8, 0) # Create example vector x # Print vector to console # 4 -10 8 0

Our example vector contains the four numeric values 4, -10, 8, and 0.

Now, we can apply **sort**…

sort(x) # Apply sort function # -10 0 4 8 |

sort(x) # Apply sort function # -10 0 4 8

…**order**…

order(x) # Apply order function # 2 4 1 3 |

order(x) # Apply order function # 2 4 1 3

…and the **rank** function to our example vector:

rank(x) # Apply rank function # 3 1 4 2 |

rank(x) # Apply rank function # 3 1 4 2

The basic R syntax of the three functions is the same. However, the output of each function is different.

Figure 1 illustrates the functioning of the sort, order, and rank functions:

**Figure 1: Comparison of sort, order & rank Functions in R.**

**Definition of sort() R function:** The sort function returns its input in ascending or descending order. As you can see in Figure 1, the lowest value (i.e. -10) of our example vector was returned first and the highest value (i.e. 8) was returned last.

**Definition of order() R function:** The order function returns the position of each element of its input in ascending or descending order. As you can see in Figure 1, the lowest value (i.e. -10) is located at position two and the highest value (i.e. 8) is located at position three within our example vector.

**Definition of rank() R function:** The rank function returns the ranking position of its input. As you can see in Figure 1, the first input element (i.e. 4) is the third lowest and the second element (i.e. – 10) is the lowest value of our example vector.

## Example 2: Order Data Frame Rows by Column Vector

The second example shows how to order the rows of a data frame by one of its columns. Let’s first create an example data frame in R:

my_data <- data.frame(x, # Create example data.frame y = LETTERS[1:4]) my_data # Print data to RStudio console # x y # 4 A # -10 B # 8 C # 0 D |

my_data <- data.frame(x, # Create example data.frame y = LETTERS[1:4]) my_data # Print data to RStudio console # x y # 4 A # -10 B # 8 C # 0 D

Our example data contains two columns: The x-column is identical to the vector that we have used in Example 1. The y-column contains the letters A, B, C, and D.

We can now use the order R function to sort our data frame by the x-column:

my_data[order(my_data$x), ] # Order data by column vector # x y # -10 B # 0 D # 4 A # 8 C |

my_data[order(my_data$x), ] # Order data by column vector # x y # -10 B # 0 D # 4 A # 8 C

As you can see based on the previous RStudio output, the rows of our example data matrix were sorted from the lowest to the highest value of the x-column.

## Example 3: Sort List by Name

The order function can also be used to sort lists by the name of each list element. Consider the following example list:

my_list <- list(B = 1:5, # Create example list C = 6:10, A = LETTERS[15:19]) my_list # Print list to RStudio console # $B # [1] 1 2 3 4 5 # # $C # [1] 6 7 8 9 10 # # $A # [1] "O" "P" "Q" "R" "S" |

my_list <- list(B = 1:5, # Create example list C = 6:10, A = LETTERS[15:19]) my_list # Print list to RStudio console # $B # [1] 1 2 3 4 5 # # $C # [1] 6 7 8 9 10 # # $A # [1] "O" "P" "Q" "R" "S"

Our example list consists of three list elements. The names of these list elements are B, C, and A.

We can now use the order command to sort our list in alphabetical order:

my_list[order(names(my_list))] # Order list alphabetically # $A # [1] "O" "P" "Q" "R" "S" # # $B # [1] 1 2 3 4 5 # # $C # [1] 6 7 8 9 10 |

my_list[order(names(my_list))] # Order list alphabetically # $A # [1] "O" "P" "Q" "R" "S" # # $B # [1] 1 2 3 4 5 # # $C # [1] 6 7 8 9 10

After applying the previous R syntax, list element A comes first, B comes second, and C comes third.

## Example 4: Sort & Order Descendingly

So far, we have sorted our data in an ascending order. However, the sort and order functions are also used to sort in descending order.

Consider the following R code for the sort function…

sort(x, decreasing = TRUE) # sort with decreasing order # 8 4 0 -10 |

sort(x, decreasing = TRUE) # sort with decreasing order # 8 4 0 -10

…and the following R code for the order function:

order(x, decreasing = TRUE) # order with decreasing order # 3 1 4 2 |

order(x, decreasing = TRUE) # order with decreasing order # 3 1 4 2

As you can see, both functions returned the same values as in Example 1 to the RStudio console, but in opposite direction.

## Example 5: Sort Data Table by Group

In R, we can also sort our data by multiple columns or vectors (i.e. by complex groups). Let’s first create some data table for the following example:

my_data2 <- data.frame(value = c(6, 2, 4, 1, 8, 5), # Create data.frame with groups group1 = c("B", "A", "B", "C", "A", "B"), group2 = c("E", "E", "E", "D", "D", "D")) my_data2 # Print data to console # value group1 group2 # 6 B E # 2 A E # 4 B E # 1 C D # 8 A D # 5 B D |

my_data2 <- data.frame(value = c(6, 2, 4, 1, 8, 5), # Create data.frame with groups group1 = c("B", "A", "B", "C", "A", "B"), group2 = c("E", "E", "E", "D", "D", "D")) my_data2 # Print data to console # value group1 group2 # 6 B E # 2 A E # 4 B E # 1 C D # 8 A D # 5 B D

Our example data contains three columns. The first column contains some numeric values that we want to order by group. The second and third columns contain letters that represent our groups.

We can sort our example data with the order function as follows:

my_data2[order(my_data2$group1, my_data2$group2), ] # Order data by group # value group1 group2 # 8 A D # 2 A E # 5 B D # 6 B E # 4 B E # 1 C D |

my_data2[order(my_data2$group1, my_data2$group2), ] # Order data by group # value group1 group2 # 8 A D # 2 A E # 5 B D # 6 B E # 4 B E # 1 C D

As you can see, we just inserted both grouping columns into the order function, which lead to an output data frame that was first sorted by the first grouping column (i.e. group1) and the by the second grouping column (i.e. group2).

## Example 6: Lesser Known Sorting Functions in R

sort, order, and rank are by far the most common functions for sorting data in R. However, there are several lesser known R sorting functions, which might also be useful in some specific scenarios.

With the **is.unsorted** function you can check whether a vector or column is sorted. The function returns the logical value TRUE in case the input is unsorted and FALSE in case the input is sorted:

is.unsorted(x) # Apply is.unsorted function # TRUE |

is.unsorted(x) # Apply is.unsorted function # TRUE

The **sort.list** function is basically the same as the order function, but instead of accepting multiple arguments it accepts only a single argument:

sort.list(x) # Apply sort.list function # 2 4 1 3 |

sort.list(x) # Apply sort.list function # 2 4 1 3

The **sort.int** function is similar to the sort function, but provides additional specification possibilities. Check the R help documentation of sort.int (i.e. type *?sort.int* to the RStudio console) for more information.

sort.int(x) # Apply sort.int function # -10 0 4 8 |

sort.int(x) # Apply sort.int function # -10 0 4 8

The **xtfrm** function produces a numeric vector which sorts in the same order as the input:

xtfrm(x) # Apply xtfrm function # 4 -10 8 0 |

xtfrm(x) # Apply xtfrm function # 4 -10 8 0

## Further Resources for Ordering & Ranking Data in R

Do you need some further examples for the sort, order, and rank functions? Then you might be interested in the following YouTube video of the Jalayer Academy. The speaker of the video shows some live examples for the application of sort, order, and rank:

**Please accept YouTube cookies to play this video.** By accepting you will be accessing content from YouTube, a service provided by an external third party.

If you accept this notice, your choice will be saved and the page will refresh.

Furthermore, I recommend to have a look on the other R programming tutorials on this website. I’m publishing new tutorials on the handling of data in R on a regular basis:

- Rank Functions of the dplyr Package
- The rev Function in R
- Sort Data Frame in R
- Sort Data Frame by Multiple Columns
- Remove List Elements in R
- R Functions List (+ Examples)
- The R Programming Language

At this point, you should know how to apply sort, order, and rank in R. However, if you have further questions or any kind of feedback, let me know in the comments below.

**5**/

**5**(

**1**vote )

### Subscribe to my free statistics newsletter:

## 2 Comments. Leave new

The output of first example is wrong:

order(x) # Apply order function

# 3 4 1 2

should be 2 4 1 3

Hey Wu Tao,

Thanks a lot for your comment, seems like I accidentally wrote the wrong numbers. I just fixed it.

Regards,

Joachim