method
ndarray.sort(axis=-1, kind=None, order=None, *, stable=None, descending=None)
Sort an array in-place. Refer to
for full documentation.
Parameters:axisint, optionalAxis along which to sort. Default is -1, which means sort along the last axis.
kind{‘quicksort’, ‘mergesort’, ‘heapsort’, ‘stable’}, optionalSorting algorithm. The default is ‘quicksort’. Note that both ‘stable’ and ‘mergesort’ use timsort under the covers and, in general, the actual implementation will vary with datatype. The ‘mergesort’ option is retained for backwards compatibility.
orderstr or list of str, optionalWhen a is an array with fields defined, this argument specifies which fields to compare first, second, etc. A single field can be specified as a string, and not all fields need be specified, but unspecified fields will still be used, in the order in which they come up in the dtype, to break ties.
stablebool, optionalSort stability. If True, the returned array will maintain the relative order of a values which compare as equal. If False or None, this is not guaranteed. Internally, this option selects kind='stable'. Default: None.
Added in version 2.0.0.
Notes
See
for notes on the different sorting algorithms.
Examples
>>> importnumpyasnp>>> a=np.array([[1,4],[3,1]])>>> a.sort(axis=1)>>> aarray([[1, 4], [1, 3]])>>> a.sort(axis=0)>>> aarray([[1, 3], [1, 4]])Use the order keyword to specify a field to use when sorting a structured array:
>>> a=np.array([('a',2),('c',1)],dtype=[('x','S1'),('y',int)])>>> a.sort(order='y')>>> aarray([(b'c', 1), (b'a', 2)], dtype=[('x', 'S1'), ('y', '<i8')])