Sorts the array by the given axis, algorithm, and order. Note that there is a difference between the sort function and the sort method:
- Returns a copy that is then sorted.
Returns None, and sorts the array in-place.
sort (arr, axis=-1, kind='quick', order=None)
arr.sort (axis=-1, kind='quick', order=None)
A sorted copy of arr.
- None. The sort is in-place and therefore affects the underlying array (no copy is made).
- Array to be sorted.
- The axis the sort is applied to in a multi-dimensional array.
The underlying algorithm to use. Also see: algorithms
- Used for arrays with fields defined. Can be one of the following:
- A single string.
- Indicates that field as the basis for the sort.
- A list or tuple of strings.
- Basis is in list order.
- If fields are defined, then the first field is the basis.
- A single string.
- The various sorts are characterized by average speed, worst case performance, need for work space, and whether they are stable. A stable sort keeps items with the same key in the same relative order. The three available algorithms have the following properties:
In : from numpy import * In : a = array([2,0,8,4,1]) In : a.sort() # in-place sorting with quicksort (default) In : a Out: array([0, 1, 2, 4, 8]) In : a.sort(kind='mergesort') # algorithm options are 'quicksort', 'mergesort' and 'heapsort' In : a = array([[8,4,1],[2,0,9]]) In : a.sort(axis=0) In : a Out: array([[2, 0, 1], [8, 4, 9]]) In : a = array([[8,4,1],[2,0,9]]) In : a.sort(axis=1) # default axis = -1 In : a Out: array([[1, 4, 8], [0, 2, 9]]) In : sort(a) # there is a functional form Out: array([[1, 4, 8], [0, 2, 9]])
Docs in the source for the function are incorrect:
sort(a, axis=-1, kind='quicksort', order=None) Return copy of 'a' sorted along the given axis. Perform an inplace sort along the given axis using the algorithm specified by the kind keyword.
It should only say it returns a copy that is sorted. It does not have any side effect of sorting the underlying array in-place.
argsort : Indirect sort.
lexsort : Indirect stable sort on multiple keys.
searchsorted : Find keys in sorted array.