Member-only story

NumPy Tutorial: NumPy Array Sorting

Jesse L
2 min readDec 30, 2021

--

Hi everyone, welcome back. NumPy is a library for the Python programming language. NumPy is short for “Numerical Python” and is a popular library that is used for data science purposes. NumPy is used specifically to work with arrays as it provides various functions and support. In this tutorial, we will go over how we can combine multiple NumPy arrays together. With this introduction out of the way, let’s get into it.

Sorting

NumPy’s sorting() function is capable of reordering the elements of an array. Based off of the elements, the order could be numerical, alphabetical, or ascending.

Let’s see an example of sorting an array containing integers:

import numpy as np
array = np.array([2, 4, 1, 3])
sortedArray = np.sort(array)
print(sortedArray)
Output:
[1 2 3 4]

As we can see in the example above, we created an array containing various integers and used the sort function on it. Our resulting output has shown us that the new sorted array has the elements ordered numerically.

More Examples

Let’s see an example of sorting an array containing strings:

import numpy as np
array = np.array(["b", "c", "d", "a"])
sortedArray = np.sort(array)
print(sortedArray)

--

--

Jesse L
Jesse L

Written by Jesse L

Hi, I'm a passionate technology enthusiast and lifelong learner. Beyond my technical pursuits, I'm also passionate about sharing my enthusiasm with others.

No responses yet