sort#

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

Return a sorted copy of an array.

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) – direction The direction in which to sort the values

  • 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 with the same dtype and shape as x, with the elements sorted along the given axis.

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([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, axis=1, descending=True, stable=False)
>>> print(y)
ivy.array([[[19. ,  5. ],[ 8.9,  0. ]],[[19. ,  0.5],[ 6. ,  0.3]]])
>>> x = ivy.array([1.5, 3.2, 0.7, 2.5])
>>> y = ivy.zeros(5)
>>> ivy.sort(x, descending=True, stable=False, out=y)
>>> print(y)
ivy.array([3.2, 2.5, 1.5, 0.7])
>>> x = ivy.array([[1.1, 2.2, 3.3],[-4.4, -5.5, -6.6]])
>>> ivy.sort(x, out=x)
>>> print(x)
ivy.array([[ 1.1,  2.2,  3.3],
    [-6.6, -5.5, -4.4]])

With ivy.Container input:

>>> x = ivy.Container(a=ivy.array([8, 6, 6]),b=ivy.array([[9, 0.7], [0.4, 0]]))
>>> y = ivy.sort(x, descending=True)
>>> print(y)
{
    a: ivy.array([8, 6, 6]),
    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, descending=False, stable=False)
>>> print(y)
{
    a: ivy.array([0.7, 1., 3.]),
    b: ivy.array([[0.9, 4.], [0.2, 0.6]])
}
Array.sort(self, /, *, axis=-1, descending=False, stable=True, out=None)[source]#

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

Return type:

Array

Examples

>>> x = ivy.array([7, 8, 6])
>>> y = x.sort(axis=-1, descending=True, stable=False)
>>> print(y)
ivy.array([8, 7, 6])
>>> x = ivy.array([8.5, 8.2, 7.6])
>>> y = x.sort(axis=-1, descending=True, stable=False)
>>> print(y)
ivy.array([8.5, 8.2, 7.6])
Container.sort(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.sort. This method simply wraps the function, and so the docstring for ivy.sort also applies to this method with minimal changes.

Return type:

Container

Examples

>>> x = ivy.Container(a=ivy.array([5, 9, 0.2]),
...                   b=ivy.array([8, 1]))
>>> y = x.sort()
>>> print(y)
{
    a: ivy.array([0.2, 5., 9.]),
    b: ivy.array([1, 8])
}
>>> x = ivy.Container(a=ivy.array([5, 9, 0.2]),
...                   b=ivy.array([[8, 1], [5, 0.8]]))
>>> y = x.sort()
>>> print(y)
{
    a: ivy.array([0.2, 5., 9.]),
    b: ivy.array([[1., 8.], [0.8, 5.]])
}
>>> x = ivy.Container(a=ivy.array([8, 0.5, 6]),
...                   b=ivy.array([[9, 0.7], [0.4, 0]]))
>>> y = ivy.sort(x)
>>> print(y)
{
    a: ivy.array([0.5, 6., 8.]),
    b: ivy.array([[0.7, 9.],[0., 0.4]])
}
>>> x = ivy.Container(a=ivy.native_array([8, 0.5, 6]),
...                   b=ivy.array([[9, 0.7], [0.4, 0]]))
>>> y = ivy.sort(x)
>>> print(y)
{
    a: ivy.array([0.5, 6., 8.]),
    b: ivy.array([[0.7, 9.],[0., 0.4]])
}