Returns the indices that would sort an array. Perform an indirect sort along the given axis using the algorithm specified by the `kind` keyword. It returns an array of indices of the same shape as `a` that index data along the given axis in sorted order. Parameters -------
(a, axis=-1, kind=None, order=None, *, stable=None, descending=np._NoValue)
| 1114 | |
| 1115 | @array_function_dispatch(_argsort_dispatcher) |
| 1116 | def argsort(a, axis=-1, kind=None, order=None, *, stable=None, descending=np._NoValue): |
| 1117 | """ |
| 1118 | Returns the indices that would sort an array. |
| 1119 | |
| 1120 | Perform an indirect sort along the given axis using the algorithm specified |
| 1121 | by the `kind` keyword. It returns an array of indices of the same shape as |
| 1122 | `a` that index data along the given axis in sorted order. |
| 1123 | |
| 1124 | Parameters |
| 1125 | ---------- |
| 1126 | a : array_like |
| 1127 | Array to sort. |
| 1128 | axis : int or None, optional |
| 1129 | Axis along which to sort. The default is -1 (the last axis). If None, |
| 1130 | the flattened array is used. |
| 1131 | kind : {'quicksort', 'mergesort', 'heapsort', 'stable'}, optional |
| 1132 | Sorting algorithm. The default is 'quicksort'. Note that both 'stable' |
| 1133 | and 'mergesort' use timsort under the covers and, in general, the |
| 1134 | actual implementation will vary with data type. The 'mergesort' option |
| 1135 | is retained for backwards compatibility. |
| 1136 | order : str or list of str, optional |
| 1137 | When `a` is an array with fields defined, this argument specifies |
| 1138 | which fields to compare first, second, etc. A single field can |
| 1139 | be specified as a string, and not all fields need be specified, |
| 1140 | but unspecified fields will still be used, in the order in which |
| 1141 | they come up in the dtype, to break ties. |
| 1142 | stable : bool, optional |
| 1143 | Sort stability. If ``True``, the returned array will maintain |
| 1144 | the relative order of ``a`` values which compare as equal. |
| 1145 | If ``False`` or ``None``, this is not guaranteed. Internally, |
| 1146 | this option selects ``kind='stable'``. Default: ``None``. |
| 1147 | |
| 1148 | .. versionadded:: 2.0.0 |
| 1149 | descending : bool, optional |
| 1150 | Sort order. If ``True``, the returned array will be sorted in |
| 1151 | descending order. If ``False`` or ``None``, the returned array will |
| 1152 | be sorted in ascending order. Values that are NaN are sorted to the |
| 1153 | end for both orders. Default: ``None``. |
| 1154 | |
| 1155 | .. versionadded:: 2.5.0 |
| 1156 | |
| 1157 | Returns |
| 1158 | ------- |
| 1159 | index_array : ndarray, int |
| 1160 | Array of indices that sort `a` along the specified `axis`. |
| 1161 | If `a` is one-dimensional, ``a[index_array]`` yields a sorted `a`. |
| 1162 | More generally, ``np.take_along_axis(a, index_array, axis=axis)`` |
| 1163 | always yields the sorted `a`, irrespective of dimensionality. |
| 1164 | |
| 1165 | See Also |
| 1166 | -------- |
| 1167 | sort : Describes sorting algorithms used. |
| 1168 | lexsort : Indirect stable sort with multiple keys. |
| 1169 | ndarray.sort : Inplace sort. |
| 1170 | argpartition : Indirect partial sort. |
| 1171 | take_along_axis : Apply ``index_array`` from argsort |
| 1172 | to an array as if by calling sort. |
| 1173 |