This tutorial will show you how to use the NumPy sort method, which is sometimes called np.sort or numpy.sort.

As the name implies, the NumPy sort technique enables you to *sort* NumPy arrays.

So, this blog post will show you exactly how to use the technique to sort different kinds of arrays in Python.

The blog post has two primary sections, a syntax explanation section and an examples section.

**Contents:**

You can click on either of those links and it will take you to the appropriate section in the tutorial.

But if you’re new to Python and NumPy, I suggest that you read the whole blog post.

Ok. Let’s just start out by talking about the sort function and where it fits into the NumPy data manipulation system.

## A quick introduction to the NumPy sort function

If you’re reading this blog post, you probably know what NumPy is.

But, just in case you don’t, I want to quickly review NumPy.

#### Numpy is a data manipulation module for Python

NumPy is a toolkit for doing data manipulation in Python.

More specifically, NumPy provides a set of tools and functions for working with arrays of numbers. That’s actually where the name comes from:

“**Num**erical **Py**thon” ….

NumPy.

#### Numpy functions work on NumPy arrays

Although the tools from NumPy can work on a variety of data structures, they are primarily designed to operate on NumPy arrays.

NumPy arrays are essentially arrays of numbers. We’ll create some NumPy arrays later in this tutorial, but you can think of them as row-and-column grids of numbers.

And again, the tools of NumPy can perform manipulations on these arrays. For example, you can do things like calculate the mean of an array, calculate the median of an array, calculate the maximum, etc.

Essentially, NumPy is a broad toolkit for working with arrays of numbers.

And one of the things you can do with NumPy, is you can *sort* an array.

#### NumPy sort sorts NumPy arrays

That’s basically what NumPy sort does … it sorts NumPy arrays.

Let me give you a quick example.

Imagine that you have a 1-dimensional NumPy array with five values that are in random order:

You can use NumPy sort to *sort* those values in ascending order. Essentially, numpy.sort will take an input array, and output a new array in sorted order.

Take a look at that image and notice what np.sort did.

It sorted the array in ascending order, from low to high. That’s it.

To be clear, the NumPy sort function can actually sort arrays in more complex ways, but at a basic level, that’s all the function does. It sorts data.

Ok … so now that I’ve explained the NumPy sort technique at a high level, let’s take a look at the details of the syntax.

## The syntax of numpy SORT

In this section, I’ll break down the syntax of np.sort.

Before I do that though, you need to be aware of some syntax conventions.

#### A quick note

When we write NumPy code, it’s very common to refer to NumPy as `np`

.

Syntactically, `np`

frequently operates as a “nickname” or alias of the NumPy package. So if you see the term `np.sort()`

, that’s sort of a shorthand for `numpy.sort()`

.

Having said that, this sort of aliasing only works if you set it up properly.

To set up that alias, you’ll need to “import” NumPy with the appropriate nickname by using the code `import numpy as np`

.

We’ll talk more about this in the examples section, but I want you to understand this before I start explaining the syntax.

#### An explanation of the syntax

Ok. Let’s take a close look at the syntax.

To initiate the function (assuming you’ve imported NumPy as I explained above), you can call the function as `np.sort()`

. Again though, you can also refer to the function as `numpy.sort()`

and it will work in a similar way.

Then inside of the function, there are a set of parameters that enable you to control exactly how the function works.

The function is fairly simple, but to really understand it, you need to understand the parameters.

With that in mind, let’s talk about the parameters of numpy.sort.

### The parameters of numpy sort

The np.sort function has 3 primary parameters:

`a`

`axis`

`kind`

There’s also a 4th parameter called `order`

. Since `order`

is not used very often and it’s a little more complicated to understand, I am leaving it out of this tutorial.

However, the parameters `a`

, `axis`

, and `kind`

are a much more common. That being the case, I’ll only explain them in a little more detail.

`a`

(required)

The `a`

parameter simply refers to the NumPy array that you want to operate on.

Typically, this will be a NumPy array object. However, np.sort (like almost all of the NumPy functions) will also operate on “array-like” objects. So for example, numpy.sort will sort Python lists, tuples, and many other itterable types.

Keep in mind that this parameter is *required*. So you need to provide a NumPy array here, or an array-like object.

`axis`

The `axis`

parameter describes the axis along which you will sort the data.

This parameter is *optional*.

By default, `axis`

is set to `axis = -1`

. This means that if you don’t use the axis parameter, then by default, the np.sort function will sort the data on the last axis.

If you’re not sure what an “axis” is, I recommend that you read our tutorial about NumPy axes. You’ll also learn more about how this parameter works in the examples section of this tutorial.

`kind`

The `kind`

parameter specifies the sorting algorithm you want to use to sort the data.

If you’re not well-trained with computer science and algorithms, you might not realize this ….

… but there are many different algorithms that can be used to sort data. Moreover, these different sorting techniques have different pros and cons. For example, some algorithms are faster than others.

So, there are several different options for this parameter: `quicksort`

, `heapsort`

, and `mergesort`

.

By default, the `kind`

parameter is set to `kind = 'quicksort'`

.

The `quicksort`

algorithm is typically sufficient for most applications, so we’re not really going to change this parameter in any of our examples. (If you have a question about sorting algorithms, just leave your question in the comments section below.)

Ok … now that you’ve learned more about the parameters of numpy.sort, let’s take a look at some working examples.

## Examples: how to use the numpy sort function

To learn and master a new technique, it’s almost always best to start with very, very simple examples.

This, by the way, is one of the mistakes that beginners make when learning new syntax; they work on examples that are simply too complicated.

Because simple examples are so important, I want to show you simple examples of how the np.sort function works.

I’ll show you how it works with NumPy arrays of different sizes …

And I’ll also show you how to use the parameters.

Here’s a list of the examples we’ll cover:

- Sort a 1D numpy array
- How to sort a Numpy array by column
- How to sort a Numpy array by row
- Sort a NumPy array in reverse order

But before you run the code in the following examples, you’ll need to make sure that everything is set up properly.

#### Run this code first

Before you run the code below, you’ll need to have NumPy installed and you’ll need to “import” the NumPy module into your environment.

Installing NumPy can be very complex, and it’s beyond the scope of this tutorial. If you don’t have it installed, you can search online for how to install it. My recommendation is to simply start using Anaconda.

###### Import numpy

Assuming that you have NumPy *installed* though, you’ll still need to run some code to import it.

To import NumPy, you can run this:

import numpy as np

This will make the NumPy functions available in your code.

Also, after running this code, you’ll be able to refer to NumPy in your code with the nickname ‘`np`

‘.

Ok … now we’re ready to go.

### Example 1: Sort a 1-dimensional NumPy array

First, we’ll start very simple.

We’re going to sort a simple, 1-dimensional numpy array.

Before we sort the array, we’ll first need to create the array. To do this, we’re going to use the np.array function. The np.array function will enable us to create a NumPy array object from a Python list of 5 numbers:

simple_array_1d = np.array([5,3,1,2,4])

And we can print out the array with a simple print statement:

print(simple_array_1d)

Which shows the following output:

array([5, 3, 1, 2, 4])

This is really simple. We just have a NumPy array of 5 numbers. As you can see, the numbers are arranged in a random order.

Next, we can sort the array with np.sort:

np.sort(simple_array_1d)

When we run this, np.sort will produce the following output array:

array([1, 2, 3, 4, 5])

As you can see, the output of np.sort is the same group of numbers, but now they are sorted in ascending order.

### Example 2: sort a numpy array by column

Next, we’re going to sort the columns of a 2-dimensional NumPy array.

To do this, we’ll first need to *create* a 2D NumPy array.

###### Create numpy array

Ultimately here, we’re going to create a 2 by 2 array of 9 integers, randomly arranged.

To do this, we’re going to use the numpy.arange function to create an array of integers from 1 to 9, then randomly arrange them with numpy random choice, and finally reshape the array into a 2 by 2 array with numpy.reshape.

np.random.seed(77) array_2d = np.random.choice(a = np.arange(start = 1, stop = 10), size = 9, replace = False).reshape([3,3])

And now let’s print out `array_2d`

to see what’s in it.

print(array_2d)

Which produces the following output:

array([[3, 6, 1], [2, 4, 7], [5, 9, 8]])

As you can see, we have a 2D array of the integers 1 to 9, arranged in a random order.

To be honest, the process for creating this array is a little complicated, so if you don’t understand it, you should review our tutorial on NumPy arrange and our tutorial on NumPy reshape.

Ok. Now let’s sort the columns of the array.

To do this, we’re going to use numpy.sort with the `axis`

parameter.

np.sort(array_2d, axis = 0)

Which produces the following NumPy array:

array([[2, 4, 1], [3, 6, 7], [5, 9, 8]])

Take a close look at the output. The columns are sorted from low to high.

Why though? Why does the `axis`

parameter do this?

To understand this example, you really need to understand NumPy axes. If you don’t understand axes, you really should read our NumPy axes tutorial.

However, I will explain axes here, briefly.

You can think of axes like *directions*.

In a 2D NumPy array, axis-0 is the direction that runs downwards down the rows and axis-1 is the direction that runs horizontally across the columns.

Once you understand this, you can understand the code `np.sort(array_2d, axis = 0)`

.

What we’re really saying here is that we want to sort the array `array_2d`

along axis 0. Remember, axis 0 is the axis that points downwards.

When we run this code, we’re basically saying that we want to sort the data in the axis-0 direction.

… effectively, this sorts the columns!

### Example 3: sort a numpy array by row

Next, let’s sort the *rows*.

Sorting the rows is very similar to sorting the columns.

To do this, we’ll need to use the `axis`

parameter again.

###### Create numpy array

Quickly though, we’ll need a NumPy array to sort.

The following code is exactly the same as the previous example (sorting the columns), so if you already ran that code, you don’t need to run it again.

np.random.seed(77) array_2d = np.random.choice(a = np.arange(start = 1, stop = 10), size = 9, replace = False).reshape([3,3])

Just so we’re clear on the contents of the array, let’s print it out again:

print(array_2d)

OUT:

array([[3, 6, 1], [2, 4, 7], [5, 9, 8]])

#### Sort the rows

Now let’s sort the rows.

Do do this, we’ll use NumPy sort with `axis = 1`

.

np.sort(array_2d, axis = 1)

Which produces the following output array, with sorted rows:

array([[1, 3, 6], [2, 4, 7], [5, 8, 9]])

Take a close look. The rows are sorted from low to high.

Once again, to understand this, you really need to understand what NumPy axes are.

As I mentioned previously in this tutorial, in a 2D array, axis 1 is the direction that runs horizontally:

So when we use the code `np.sort(array_2d, axis = 1)`

, we’re telling NumPy that we want to sort the data along that axis-1 direction.

This basically means, sort the rows!

### Example 4: Sort a numpy array in reverse order

A common question that people ask when they dive further into NumPy is “how can I sort the data in reverse order?”

Unfortunately, this is not so easy to do.

I think that there should be a way to do this directly with NumPy, but at the moment, there isn’t.

That being the case, I’ll show you a quick-and-dirty workaround.

(But note: this is not necessarily an *efficient* workaround.)

###### Sort a 1D array in descending order

We’re going to sort our 1D array `simple_array_1d`

that we created above.

Let’s print out `simple_array_1d`

to see what’s in it.

print(simple_array_1d)

OUT:

[5 3 1 2 4]

You can see that this is a NumPy array with 5 elements that are arranged in random order.

Now, we’re going to sort these values in *reverse* order.

To do this, we’re going to use np.sort on the negative of the values in `array2d`

(i.e., `-array_2d`

), and we’ll take the negative of that output:

-np.sort(-array_2d)

Which gives us the following result:

array([5, 4, 3, 2, 1])

You can see that the code `-np.sort(-array_2d)`

sorted the numbers in reverse (i.e., descending) order.

###### Sort the columns of a 2D array in descending order

You can use this technique in a similar way to sort the columns and rows in descending order.

To do this, we need to use the axis parameter in conjunction with the technique we used in the previous section.

To sort the columns, we’ll need to set `axis = 0`

. And we’ll use the negative sign to sort our 2D array in reverse order.

-np.sort(-array_2d, axis = 0)

Which produces the following output:

array([[9, 7, 5], [8, 4, 3], [6, 2, 1]])

As you can see, the code `-np.sort(-array_2d, axis = 0)`

produces an output array where the columns have been sorted in descending order, from the top of the column to the bottom.

###### Sort the rows of a 2D array in descending order

You can do the same thing to sort the rows by using `axis = 1`

.

Again, we’ll be working with `array_2d`

.

-np.sort(-array_2d, axis = 1)

The code `axis = 1`

indicates that we’ll be sorting the data in the axis-1 direction, and by using the negative sign in front of the array name and the function name, the code will sort the rows in descending order.

## Learn more about NumPy and data science in Python

Here in this tutorial, I’ve explained how to sort numpy arrays by using the np.sort function.

But the NumPy toolkit is much bigger than one function.

If you’re serious about data science and scientific computing in Python, you’ll have to learn quite a bit more about NumPy.

In fact, if you want to master data science in Python, you’ll need to learn quite a few Python packages. You’ll need to learn NumPy, Pandas, matplotlib, scikit learn, and more.

There’s a lot to learn!

If you’re ready to learn data science though, we can help.

## For more tutorials, sign up for our email list

Here at Sharp Sight, we teach data science.

We offer premium data science courses to help you master data science *fast* …

And we also offer FREE tutorials.

If you sign up for our email list, you’ll get our free tutorials, and you’ll find out when our courses open for registration.

When you sign up, you’ll get free tutorials on:

- NumPy
- Pandas
- Base Python
- Scikit learn
- Machine learning
- Deep learning
- Data science in Python
- Data science in R
- … and more.

If you want access to our free tutorials every week, enter your email address and sign up now.