Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Sorting Arrays | Commonly used NumPy Functions
Ultimate NumPy
course content

Course Content

Ultimate NumPy

Ultimate NumPy

1. NumPy Basics
2. Indexing and Slicing
3. Commonly used NumPy Functions
4. Math with NumPy

book
Sorting Arrays

This operation is extremely useful since searching in a sorted array is much faster because efficient algorithms like binary search work only with sorted arrays.

numpy.sort() function

NumPy has a built-in function sort() for sorting elements by values in ascending order. The return value of this function is a sorted NumPy array. Here is its general syntax: numpy.sort(a, axis=-1, kind=None, order=None), where:

  • a is an array;
  • axis is the axis along which to sort (last axis (-1) by default);
  • kind is the sorting algorithm to use (quicksort by default).
123
import numpy as np array_1d = np.array([10, 2, 5, 1, 6, 5]) print(np.sort(array_1d))
copy

ndarray.sort() method

As we already mentioned, the numpy.sort() function returns a sorted array but does not change the original array. If we wanted to change the array, we would have to write array = np.sort(array).

However, NumPy provides a .sort() method as an alternative, which sorts the array in-place and does not return a new array (it returns None, meaning it doesn't return anything). Its syntax is similar to the sort() function.

Note

A function is a standalone block of code that performs a specific task and can be called directly. A method is a function that is associated with an object and is called on that object, using the . operator.

12345
import numpy as np array_1d = np.array([10, 2, 5, 1, 6, 5]) # Calling the .sort() method array_1d.sort() print(array_1d)
copy

After calling the .sort() method, array_1d was sorted in place and now contains elements sorted in ascending order.

Sorting 1D Arrays in Descending Order

Sometimes we may want to sort an array in descending order. Neither the .sort() method nor the sort() function supports this functionality directly. However, we can simply use slicing with step equal to -1 on a sorted array:

12345
import numpy as np array_1d = np.array([10, 2, 5, 1, 6, 5]) # Sorting array_1d in descending order array_1d = np.sort(array_1d)[::-1] print(array_1d)
copy
Task
test

Swipe to show code editor

You are managing a dataset of employee salaries stored in the salaries array.

  1. Sort the salaries in descending order using the appropriate function.
  2. Print the top 3 salaries using a slice and specifying only a positive end.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 1
toggle bottom row

book
Sorting Arrays

This operation is extremely useful since searching in a sorted array is much faster because efficient algorithms like binary search work only with sorted arrays.

numpy.sort() function

NumPy has a built-in function sort() for sorting elements by values in ascending order. The return value of this function is a sorted NumPy array. Here is its general syntax: numpy.sort(a, axis=-1, kind=None, order=None), where:

  • a is an array;
  • axis is the axis along which to sort (last axis (-1) by default);
  • kind is the sorting algorithm to use (quicksort by default).
123
import numpy as np array_1d = np.array([10, 2, 5, 1, 6, 5]) print(np.sort(array_1d))
copy

ndarray.sort() method

As we already mentioned, the numpy.sort() function returns a sorted array but does not change the original array. If we wanted to change the array, we would have to write array = np.sort(array).

However, NumPy provides a .sort() method as an alternative, which sorts the array in-place and does not return a new array (it returns None, meaning it doesn't return anything). Its syntax is similar to the sort() function.

Note

A function is a standalone block of code that performs a specific task and can be called directly. A method is a function that is associated with an object and is called on that object, using the . operator.

12345
import numpy as np array_1d = np.array([10, 2, 5, 1, 6, 5]) # Calling the .sort() method array_1d.sort() print(array_1d)
copy

After calling the .sort() method, array_1d was sorted in place and now contains elements sorted in ascending order.

Sorting 1D Arrays in Descending Order

Sometimes we may want to sort an array in descending order. Neither the .sort() method nor the sort() function supports this functionality directly. However, we can simply use slicing with step equal to -1 on a sorted array:

12345
import numpy as np array_1d = np.array([10, 2, 5, 1, 6, 5]) # Sorting array_1d in descending order array_1d = np.sort(array_1d)[::-1] print(array_1d)
copy
Task
test

Swipe to show code editor

You are managing a dataset of employee salaries stored in the salaries array.

  1. Sort the salaries in descending order using the appropriate function.
  2. Print the top 3 salaries using a slice and specifying only a positive end.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 1
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt