DEV Community

TechPlygrnd
TechPlygrnd

Posted on

NumPy Tutorial #12: Array Sorting

In this blog, I will show you how to sort an array in NumPy.


Sort

NumPy has provided a method called sort to sort an array. This method requires one argument which is the original array that you want to sort. Let see how that method works in code.

Let us create an array

import numpy as np

unsorted_arr = np.array([2, 5, 1, 7, 8, 9, 3, 4, 6, 7])
Enter fullscreen mode Exit fullscreen mode

Then, use the method sort to sort the unsorted_arr

sorted_arr = np.sort(unsorted_arr)
Enter fullscreen mode Exit fullscreen mode

Finally, print the sorted_arr to see the output

print(sorted_arr)
Enter fullscreen mode Exit fullscreen mode

You will see the following output

[1 2 3 4 5 6 7 7 8 9]
Enter fullscreen mode Exit fullscreen mode

That is how you can sort an array in NumPy. Thank you for reading, and have a nice day!

Top comments (0)