Sorting
- ivy.argsort(x, axis=-1, descending=False, stable=True, *, out=None)[source]
Returns the indices that sort an array
x
along a specified axis.- Parameters
x (
Union
[Array
,NativeArray
]) – input array.axis (
int
) – axis along which to sort. If set to-1
, the function must sort along the (default:-1
) last axis. Default:-1
.descending (
bool
) – sort order. IfTrue
, the returned indices sortx
in descending order (default:False
) (by value). IfFalse
, the returned indices sortx
in ascending order (by value). Default:False
.stable (
bool
) – sort stability. IfTrue
, the returned indices must maintain the relative (default:True
) order ofx
values which compare as equal. IfFalse
, the returned indices may or may not maintain the relative order ofx
values which compare as equal (i.e., the relative order ofx
values which compare as equal is implementation-dependent). Default:True
.out (
Optional
[Array
]) – optional output array, for writing the result to. It must have the same shape (default:None
) asx
.
- Return type
- Returns
ret – an array of indices. The returned array must have the same shape as
x
. The returned array must have the default array index data type.This method conforms to the `Array API Standard
<https (//data-apis.org/array-api/latest/>`_. This docstring is an extension of the)
`docstring <https (//data-apis.org/array-api/latest/API_specification/generated/) – signatures.elementwise_functions.tan.html>`_
in the standard. The descriptions above assume an array input for simplicity, but
the method also accepts
ivy.Container
instances in place ofivy.Array
orivy.NativeArray
instances, as shown in the type hintsand also the examples below.
Examples
With: code:ivy.Array input:
>>> x = ivy.array([3,1,2]) >>> y = ivy.argsort(x) >>> print(y) ivy.array([1,2,0])
>>> x = ivy.array([[1.5, 3.2], [2.3, 2.3]]) >>> ivy.argsort(x, 0, True, False, out=x) >>> print(x) ivy.array([[1, 0], [0, 1]])
>>> x = ivy.array([[[1,3], [3,2]], [[2,4], [2,0]]]) >>> y = ivy.argsort(x, 1, False, True) >>> print(y) ivy.array([[[0, 1], [1, 0]], [[0, 1], [1, 0]]])
With
ivy.Container
input:>>> x = ivy.Container(a=ivy.array([5,1,3]), b=ivy.array([[0, 3], [3, 2]])) >>> y = ivy.argsort(x) >>> print(y) { a: ivy.array([1, 2, 0]), b: ivy.array([[0, 1], [1, 0]]) }
- ivy.searchsorted(x, v, side='left', sorter=None, *, out=None)[source]
Returns the indices of the inserted elements in a sorted array.
- Parameters
x (
Union
[Array
,NativeArray
]) – input arrayv (
Union
[Array
,NativeArray
]) – specific elements to insert in array x1side – The specific elements’ index is at the ‘left’ side or ‘right’ side in the sorted array x1. If the side is ‘left’, the index of the first suitable location located is given. If ‘right’, return the last such index.
out (
Optional
[Array
]) – optional output array, for writing the result to. (default:None
)
- Return type
- Returns
ret – An array of insertion points.
Examples
With:code:ivy.Array inputs:
>>> x1 = ivy.array([2,1,0]) >>> x2 = ivy.array([1]) >>> y = ivy.searchsorted(x1,x2) >>> print(y) ivy.array([0])
>>> x1 = ivy.array([1,0,3,2]) >>> x2 = ivy.array([3]) >>> y = ivy.searchsorted(x1, x2, side='right') >>> print(y) ivy.array([4])
>>> x1 = ivy.array([2,0,1,3]) >>> x2 = ivy.array([3,1,9]) >>> y = ivy.searchsorted(x1, x2, side='left') >>> print(y) ivy.array([3,2,4])
- ivy.sort(x, axis=-1, descending=False, stable=True, *, out=None)[source]
Returns a sorted copy of an array.
- Parameters
x (
Union
[Array
,NativeArray
]) – input arrayaxis (
int
) – axis along which to sort. If set to-1
, the function must sort along the (default:-1
) last axis. Default:-1
.descending (
bool
) – direction The direction in which to sort the values (default:False
)stable (
bool
) – sort stability. IfTrue
, (default:True
) the returned indices must maintain the relative order ofx
values which compare as equal. IfFalse
, the returned indices may or may not maintain the relative order ofx
values which compare as equal (i.e., the relative order ofx
values which compare as equal is implementation-dependent). Default:True
.out (
Optional
[Union
[Array
,NativeArray
]]) – optional output array, for writing the result to. It must have a shape that the (default:None
) inputs broadcast to.
- Return type
- Returns
ret – An array with the same dtype and shape as values, with the elements sorted along the given axis.
Examples
With:code:ivy.Array inputs:
>>> x = ivy.array([7, 8, 6]) >>> y = ivy.sort(x) >>> print(y) ivy.array([6, 7, 8])
>>> x = ivy.array([[[8.9,0], [19,5]], [[6,0.3], [19,0.5]]]) >>> y = ivy.sort(x, -1, True, False) >>> print(y) ivy.array([[[ 8.9, 0. ], [19. , 5. ]], [[ 6. , 0.3], [19. , 0.5]]])
With:code:ivy.NativeArray inputs:
>>> x = ivy.native_array([1.5, 3.2, 0.7, 2.5]) >>> y = ivy.sort(x, -1, True, False) >>> print(y) ivy.array([3.2, 2.5, 1.5, 0.7])
>>> x = ivy.native_array([[[8.9, 0], [19, 5]], [[6, 0.3], [19, 0.5]]]) >>> y = ivy.sort(x, -1, True, False) >>> print(y) ivy.array([[[ 8.9, 0.],[19. , 5. ]], [[ 6. , 0.3 ],[19. , 0.5]]])
With a mix of
ivy.Container
andivy.Array
input:>>> x = ivy.Container(a=ivy.array([8, 0.5, 6]), b=ivy.array([[9, 0.7], [0.4, 0]])) >>> y = ivy.sort(x, -1, True, False) >>> print(y) { a: ivy.array([8., 6., 0.5]), b: ivy.array([[9., 0.7], [0.4, 0.]]) }
>>> x = ivy.Container(a=ivy.array([3, 0.7, 1]), b=ivy.array([[4, 0.9], [0.6, 0.2]])) >>> y = ivy.sort(x, -1, True, False) >>> print(y) { a: ivy.array([3., 1., 0.7]), b: ivy.array([[4., 0.9], [0.6, 0.2]]) }
- With a mix of
ivy.Container`and :code:`ivy.Array
and
ivy.NativeArray
input:
>>> x = ivy.Container(a=ivy.array([8, 0.5, 6]), b=ivy.native_array([[9, 0.7], [0.4, 0]])) >>> y = ivy.sort(x, -1, True, False) >>> print(y) { a: ivy.array([8., 6., 0.5]), b: ivy.array([[9., 0.7], [0.4, 0.]]) }
>>> x = ivy.Container(a=ivy.array([3, 0.9, 5]), b=ivy.native_array([[4, 0.1], [0.4, 0.8]])) >>> y = ivy.sort(x, -1, True, False) >>> print(y) { a: ivy.array([5., 3., 0.9]), b: ivy.array([[4., 0.1], [0.8, 0.4]]) }