argsort#

ivy.argsort(x, /, *, axis=-1, descending=False, stable=True, out=None)[source]#

Return the indices that sort an array x along a specified axis.

Parameters:
  • x (Union[Array, NativeArray]) – input array.

  • axis (int, default: -1) – axis along which to sort. If set to -1, the function must sort along the last axis. Default: -1.

  • descending (bool, default: False) – sort order. If True, the returned indices sort x in descending order (by value). If False, the returned indices sort x in ascending order (by value). Default: False.

  • stable (bool, default: True) – sort stability. If True, the returned indices must maintain the relative order of x values which compare as equal. If False, the returned indices may or may not maintain the relative order of x values which compare as equal (i.e., the relative order of x values which compare as equal is implementation-dependent). Default: True.

  • out (Optional[Array], default: None) – optional output array, for writing the result to. It must have the same shape as x.

Return type:

Array

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 function conforms to the Array API Standard. This docstring is an extension of the docstring in the standard.

Both the description and the type hints above assumes an array input for simplicity, but this function is nestable, and therefore also accepts ivy.Container instances in place of any of the arguments

Examples

With ivy.Array input:

>>> x = ivy.array([3,1,2])
>>> y = ivy.argsort(x)
>>> print(y)
ivy.array([1,2,0])
>>> x = ivy.array([4,3,8])
>>> y = ivy.argsort(x, descending=True)
>>> print(y)
ivy.array([2,0,1])
>>> x = ivy.array([[1.5, 3.2], [2.3, 2.3]])
>>> ivy.argsort(x, axis=0, descending=True, stable=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, axis=1, descending=False, stable=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]])
}
>>> x = ivy.Container(a=ivy.array([[3.5, 5],[2.4, 1]]))
>>> y = ivy.argsort(x)
>>> print(y)
{
    a: ivy.array([[0,1],[1,0]])
}
>>> x = ivy.Container(a=ivy.array([4,3,6]), b=ivy.array([[4, 5], [2, 4]]))
>>> y = ivy.argsort(x, descending=True)
>>> print(y)
{
    a: ivy.array([2, 0, 1]),
    b: ivy.array([[1, 0], [1, 0]])
}
>>> x = ivy.Container(a=ivy.array([[1.5, 3.2],[2.3, 4]]),
...                   b=ivy.array([[[1,3],[3,2],[2,0]]]))
>>> y = x.argsort(axis=-1, descending=True, stable=False)
>>> print(y)
{
    a: ivy.array([[1,0],[1,0]]),
    b: ivy.array([[[1,0],[0, 1],[0, 1]]])
}
Array.argsort(self, /, *, axis=-1, descending=False, stable=True, out=None)[source]#

ivy.Array instance method variant of ivy.argsort. This method simply wraps the function, and so the docstring for ivy.argsort also applies to this method with minimal changes.

Parameters:
  • self (Array) – input array.

  • axis (int, default: -1) – axis along which to sort. If set to -1, the function must sort along the last axis. Default: -1.

  • descending (bool, default: False) – sort order. If True, the returned indices sort x in descending order (by value). If False, the returned indices sort x in ascending order (by value). Default: False.

  • stable (bool, default: True) – sort stability. If True, the returned indices must maintain the relative order of x values which compare as equal. If False, the returned indices may or may not maintain the relative order of x values which compare as equal (i.e., the relative order of x values which compare as equal is implementation-dependent). Default: True.

  • out (Optional[Array], default: None) – optional output array, for writing the result to. It must have the same shape as input.

Return type:

Array

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.

Examples

>>> x = ivy.array([1, 5, 2])
>>> y = x.argsort(axis=-1, descending=True, stable=False)
>>> print(y)
ivy.array([1, 2, 0])
>>> x = ivy.array([9.6, 2.7, 5.2])
>>> y = x.argsort(axis=-1, descending=True, stable=False)
>>> print(y)
ivy.array([0, 2, 1])
Container.argsort(self, /, *, axis=-1, descending=False, stable=True, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, out=None)[source]#

ivy.Container instance method variant of ivy.argsort. This method simply wraps the function, and so the docstring for ivy.argsort also applies to this method with minimal changes.

Parameters:
  • self (Container) – input array or container. Should have a numeric data type.

  • axis (Union[int, Container], default: -1) – axis along which to sort. If set to -1, the function must sort along the last axis. Default: -1.

  • descending (Union[bool, Container], default: False) – sort order. If True, the returned indices sort x in descending order (by value). If False, the returned indices sort x in ascending order (by value). Default: False.

  • stable (Union[bool, Container], default: True) – sort stability. If True, the returned indices must maintain the relative order of x values which compare as equal. If False, the returned indices may or may not maintain the relative order of x values which compare as equal (i.e., the relative order of x values which compare as equal is implementation-dependent). Default: True.

  • key_chains (Optional[Union[List[str], Dict[str, str], Container]], default: None) – The key-chains to apply or not apply the method to. Default is None.

  • to_apply (Union[bool, Container], default: True) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default is True.

  • prune_unapplied (Union[bool, Container], default: False) – Whether to prune key_chains for which the function was not applied. Default is False.

  • map_sequences (Union[bool, Container], default: False) – Whether to also map method to sequences (lists, tuples). Default is False.

  • out (Optional[Container], default: None) – optional output container, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Container

Returns:

ret – a container containing the index values of sorted array. The returned array must have a data type determined by type-promotion.

Examples

>>> x = ivy.Container(a=ivy.array([7, 2, 1]),
...                   b=ivy.array([3, 2]))
>>> y = x.argsort(axis=-1, descending=True, stable=False)
>>> print(y)
{
    a: ivy.array([0, 1, 2]),
    b: ivy.array([0, 1])
}