This error occurs when you try to sort a list without first using the unlist() function.

To solve this error, you must use the unlist() function before calling sort(), for example:


This tutorial will go through the error and how to solve it with code examples.

The Sort function in R

The sort function sorts a vector a factor into ascending or descending order. sort expects a class, or a numeric, complex, character or logical vector. expects a numeric, complex, character, or logical vector or a factor. If you try to pass a list to sort instead of a vector or a factor you will raise the error, na.last = na.last, decreasing = decreasing, …) : ‘x’.

Example:, na.last = na.last, decreasing = decreasing, …) : ‘x’ must be atomic

Let’s look at an example of a list of numbers.

numbers <- list(10, 7, 8, 1, 20, 4, 8)

We can verify that the numbers object is a list using the class() function.

[1] "list"

We will attempt to sort the list in ascending order using the sort() function.


Let’s run the code to see the result:

Error in, na.last = na.last, decreasing = decreasing, ...) : 
  'x' must be atomic

The error occurs because the sort function is only capable of sorting atomic objects like vectors.


We can solve the error by using the unlist() function to convert the list to a vector. We can verify the unlist() function call returns an atomic vector using the is.atomic and is.vector functions:

numbers <- list(10, 7, 8, 1, 20, 4, 8)
vec <- unlist(numbers)
[1] TRUE
[1] TRUE

Now that we have an atomic vector we can pass it to the sort function:


Let’s run the code to see the result:

[1]  1  4  7  8  8 10 20

We can put the above solution all in one line as follows:

[1]  1  4  7  8  8 10 20

If we want to sort in descending order we can pass decreasing=TRUE as the second argument of the sort function call:

sort(unlist(numbers), decreasing=TRUE)

Let’s run the code to get the result:

[1] 20 10  8  8  7  4  1


Congratulations on reading to the end of this tutorial!

For further reading on R related errors, go to the articles: 

Go to the online courses page on R to learn more about coding in R for data science and machine learning.

Have fun and happy researching!