Sort Character Vector in R (3 Examples)


In this R programming tutorial you’ll learn how to sort a character vector.

Table of contents:

Here’s how to do it:


Example Data

Let’s first create some example data:

x <- c("aaa", "b", "a", "a", "c", "b123")      # Create example vector
x                                              # Print example vector
# [1] "aaa"  "b"    "a"    "a"    "c"    "b123"

The previously shown output of the RStudio console shows that the example data is a vector containing six character string elements. Some of the elements contain letters and numbers.


Example 1: Sort Character Vector Alphabetically Using sort() Function

Example 1 explains how to sort a vector of character strings alphabetically using Base R.

For this task, we can apply the sort function as shown below:

x_sort1 <- sort(x)                             # Sort alphabetically
x_sort1                                        # Print sorted vector
# [1] "a"    "a"    "aaa"  "b"    "b123" "c"

The previous RStudio console output shows that we have created a new vector object called x_sort1 that contains our input character strings in alphabetical order.


Example 2: Sort Character Vector Alphabetically Using str_sort() Function of stringr Package

In Example 2, I’ll show how to use the stringr package to sort a vector alphabetically.

First, we have to install and load the stringr package:

install.packages("stringr")                    # Install stringr package
library("stringr")                             # Load stringr package

Next, we can apply the str_sort function to order our vector of character strings:

x_sort2 <- str_sort(x)                         # Sort alphabetically
x_sort2                                        # Print sorted vector
# [1] "a"    "a"    "aaa"  "b"    "b123" "c"

As you can see, the previous output is exactly the same as in Example 1. However, this time we have used the stringr package instead of Base R.


Example 3: Sort Character Vector According to Specific Order

This example explains how to reorder a vector according to a manually specified order.

For this, we first have to create a new vector object that reflects the desired ordering:

my_order <- c("b", "aaa", "c", "b123", "a")    # Create manual order
my_order                                       # Print manual order
# [1] "b"    "aaa"  "c"    "b123" "a"

Now, we can use the match function to order our example vector according to our manually specified order:

x_sort3 <- x[order(match(x, my_order))]        # Sort according to manual order
x_sort3                                        # Print sorted vector
# [1] "b"    "aaa"  "c"    "b123" "a"    "a"

The previous R syntax has constructed a vector called x_sort3, which contains our input values in a manually specified order.


Video, Further Resources & Summary

Do you need more explanations on the topics of this tutorial? Then I recommend watching the following video on my YouTube channel. In the video, I’m explaining the topics of this article in RStudio.



In addition to the video, you could read the other tutorials on


Summary: In this R tutorial you have learned how to order a character vector. In case you have any further questions, let me know in the comments below.


Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.