In this article, we will learn how to sort a Numpy array. There are multiple ways in Numpy to sort an array, based on the requirement. Let’s try to understand them with the help of examples.
Example #1: Simply sort the given array based on axis using sort() method.
Along first axis : [[10 1] [12 15]] Along first axis : [[10 15] [ 1 12]] Along none axis : [ 1 10 12 15]
Example #2: Get the indices which can return sorted array using argsort() method
Original array: [9 3 1 7 4 3 6] Sorted indices of original array-> [2 1 5 4 6 3 0] Sorted array-> [1 3 3 4 6 7 9]
Example #3: Get stable sort using a sequence of keys.
column a, column b 9 4 3 6 1 9 3 2 4 1 3 8 6 7 Sorted indices-> [2 3 1 5 4 6 0]
- numpy.sort() in Python
- Python | Numpy matrix.sort()
- Python | Flatten a 2d numpy array into 1d array
- Python | Multiply 2d numpy array corresponding to 1d array
- Python | Reverse a numpy array
- Python: Numpy's Structured Array
- Python | Ways to add row/columns in numpy array
- Python | Find Mean of a List of Numpy Array
- Python | Replace negative value with zero in numpy array
- Create a Numpy array filled with all zeros | Python
- Create a Numpy array with random values | Python
- Python | Filter out integers from float numpy array
- Python | dtype object length of Numpy array of strings
- Python | Numpy numpy.ndarray.__rshift__()
- Python | Numpy numpy.ndarray.__mul__()
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.