NumPy | msort method
Start your free 7-days trial now!
Numpy's msort(~)
method returns a new copy of the input array that is sorted along the first axis. Note that this method is equivalent to np.sort(array,axis=0)
.
Parameters
1. a
| array_like
The input array.
Return value
A Numpy array that is a copy of the input array sorted along the first axis.
The msort(~)
method lacks flexibility
The msort(~)
method can only sort along the first axis, which means that for 2D arrays, sorting can only be done column-wise. If you require a more flexible method, then go for np.sort(~)
- click here for its documentation.
Examples
Sorting an one-dimensional array
x = np.array([5,7,2,4])np.msort(x)
array([2, 4, 5, 7])
Sorting a two-dimensional array
As mentioned above, the msort(~)
method can only sort elements along the first axis. This means that for 2D arrays, we can only sort column-wise.
Suppose we have the following:
x = np.array([[1,4],[3,2]])x
array([[1, 4], [3, 2]])
To perform column-wise sort:
np.msort(x)
array([[1, 2], [3, 4]])
If you need to sort row-wise, use np.sort(x, axis=1)
instead.