How to use sorting in Numpy?
Ask by ICSM Computer
Updated 18 Nov 2025
1. Basic Sorting
Output:
2. Sorting a 2D Array
By default, NumPy sorts rows individually.
Output:
3. Sorting by Axis
Sort each column
Sort each row
4. Using
np.argsort()(Get Sorted Indices)argsort()returns indices of sorted elements.Output:
You can use it to rearrange the array:
5. Sorting Structured Arrays (Sort by Field)
6. In-place Sorting
You can sort an array without creating a copy:
7. Sorting in Descending Order
NumPy does not have a direct parameter like
reverse=True,but you can reverse after sorting:
Summary
np.sort()arr.sort()np.argsort()np.sort(arr, axis=0/1)np.sort(order='field')