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

Conteúdo do Curso

Ultimate NumPy

Ultimate NumPy

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

bookSorting Arrays

Let’s start with perhaps the most common operation for arrays: sorting.

This is extremely useful for searching an element in an array since searching in a sorted array is much faster than in an unsorted one.

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 our array;
  • axis is the axis along which to sort (last axis (-1) by default);
  • kind is the sorting algorithm to use (quicksort by default).

You will most likely rarely specify the kind parameter and even less often the order parameter, so we won’t discuss them here.

Let’s first have a look at an example with a 1D array:

123
import numpy as np array_1d = np.array([10, 2, 5, 1, 6, 5]) print(np.sort(array_1d))
copy

Here is the visualization:

As you can see, with a 1D array, we only need to specify the a parameter, and that’s enough.

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 has a .sort() method as an alternative, which sorts the array in-place and returns None. Its syntax is completely identical to the sort() function.

Here is an example:

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

Let's take a look at the visualization:

Tarefa

You are managing a dataset of employee salaries stored in the salaries array. Your task is the following:

  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 desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 3. Capítulo 1
toggle bottom row

bookSorting Arrays

Let’s start with perhaps the most common operation for arrays: sorting.

This is extremely useful for searching an element in an array since searching in a sorted array is much faster than in an unsorted one.

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 our array;
  • axis is the axis along which to sort (last axis (-1) by default);
  • kind is the sorting algorithm to use (quicksort by default).

You will most likely rarely specify the kind parameter and even less often the order parameter, so we won’t discuss them here.

Let’s first have a look at an example with a 1D array:

123
import numpy as np array_1d = np.array([10, 2, 5, 1, 6, 5]) print(np.sort(array_1d))
copy

Here is the visualization:

As you can see, with a 1D array, we only need to specify the a parameter, and that’s enough.

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 has a .sort() method as an alternative, which sorts the array in-place and returns None. Its syntax is completely identical to the sort() function.

Here is an example:

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

Let's take a look at the visualization:

Tarefa

You are managing a dataset of employee salaries stored in the salaries array. Your task is the following:

  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 desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 3. Capítulo 1
toggle bottom row

bookSorting Arrays

Let’s start with perhaps the most common operation for arrays: sorting.

This is extremely useful for searching an element in an array since searching in a sorted array is much faster than in an unsorted one.

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 our array;
  • axis is the axis along which to sort (last axis (-1) by default);
  • kind is the sorting algorithm to use (quicksort by default).

You will most likely rarely specify the kind parameter and even less often the order parameter, so we won’t discuss them here.

Let’s first have a look at an example with a 1D array:

123
import numpy as np array_1d = np.array([10, 2, 5, 1, 6, 5]) print(np.sort(array_1d))
copy

Here is the visualization:

As you can see, with a 1D array, we only need to specify the a parameter, and that’s enough.

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 has a .sort() method as an alternative, which sorts the array in-place and returns None. Its syntax is completely identical to the sort() function.

Here is an example:

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

Let's take a look at the visualization:

Tarefa

You are managing a dataset of employee salaries stored in the salaries array. Your task is the following:

  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 desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Let’s start with perhaps the most common operation for arrays: sorting.

This is extremely useful for searching an element in an array since searching in a sorted array is much faster than in an unsorted one.

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 our array;
  • axis is the axis along which to sort (last axis (-1) by default);
  • kind is the sorting algorithm to use (quicksort by default).

You will most likely rarely specify the kind parameter and even less often the order parameter, so we won’t discuss them here.

Let’s first have a look at an example with a 1D array:

123
import numpy as np array_1d = np.array([10, 2, 5, 1, 6, 5]) print(np.sort(array_1d))
copy

Here is the visualization:

As you can see, with a 1D array, we only need to specify the a parameter, and that’s enough.

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 has a .sort() method as an alternative, which sorts the array in-place and returns None. Its syntax is completely identical to the sort() function.

Here is an example:

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

Let's take a look at the visualization:

Tarefa

You are managing a dataset of employee salaries stored in the salaries array. Your task is the following:

  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 desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Seção 3. Capítulo 1
Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
some-alt