Sort

As in Python’s list, NumPy arrays can be sorted in-place using the numpy.sort method. You can sort any one-dimensional section of values in a multidimensional array in place along an axis by passing the axis number to sort:

[1]:
import numpy as np


data = np.random.randn(7, 3)

data
[1]:
array([[-0.61482053,  0.17896449, -1.29336591],
       [ 0.08364197,  0.67494142, -0.59437682],
       [-1.72673297,  0.96615644,  1.60456092],
       [ 0.98478964, -0.70769764,  0.60908458],
       [ 2.572301  ,  1.20356725,  0.10333016],
       [-0.57424083,  0.87287599,  0.47258124],
       [ 1.2490576 ,  0.99737005, -0.73766589]])
[2]:
data.sort(0)

data
[2]:
array([[-1.72673297, -0.70769764, -1.29336591],
       [-0.61482053,  0.17896449, -0.73766589],
       [-0.57424083,  0.67494142, -0.59437682],
       [ 0.08364197,  0.87287599,  0.10333016],
       [ 0.98478964,  0.96615644,  0.47258124],
       [ 1.2490576 ,  0.99737005,  0.60908458],
       [ 2.572301  ,  1.20356725,  1.60456092]])

np.sort, on the other hand, returns a sorted copy of an array instead of changing the array in place:

[3]:
np.sort(data, axis=1)
[3]:
array([[-1.72673297, -1.29336591, -0.70769764],
       [-0.73766589, -0.61482053,  0.17896449],
       [-0.59437682, -0.57424083,  0.67494142],
       [ 0.08364197,  0.10333016,  0.87287599],
       [ 0.47258124,  0.96615644,  0.98478964],
       [ 0.60908458,  0.99737005,  1.2490576 ],
       [ 1.20356725,  1.60456092,  2.572301  ]])
[4]:
data
[4]:
array([[-1.72673297, -0.70769764, -1.29336591],
       [-0.61482053,  0.17896449, -0.73766589],
       [-0.57424083,  0.67494142, -0.59437682],
       [ 0.08364197,  0.87287599,  0.10333016],
       [ 0.98478964,  0.96615644,  0.47258124],
       [ 1.2490576 ,  0.99737005,  0.60908458],
       [ 2.572301  ,  1.20356725,  1.60456092]])