Sorting#

class ivy.data_classes.container.sorting._ContainerWithSorting(dict_in=None, queues=None, queue_load_sizes=None, container_combine_method='list_join', queue_timeout=None, print_limit=10, key_length_limit=None, print_indent=4, print_line_spacing=0, ivyh=None, default_key_color='green', keyword_color_dict=None, rebuild_child_containers=False, types_to_iteratively_nest=None, alphabetical_keys=True, dynamic_backend=None, **kwargs)[source]#

Bases: ContainerBase

_abc_impl = <_abc_data object>#
static _static_argsort(x, /, *, axis=-1, descending=False, stable=True, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, out=None)[source]#

ivy.Container static 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:
  • x (Union[Array, NativeArray, Container]) – input array or container. Should have a numeric data type.

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

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

  • stable (bool) – sort stability. If True, the returned indices must maintain (default: True) 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]]]) – The key-chains to apply or not apply the method to. Default is None. (default: None)

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

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

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

  • out (Optional[Container]) – optional output container, for writing the result to. It must have a shape (default: None) 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

With ivy.Container input:

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

With ivy.Container input:

>>> x = ivy.Container(a=ivy.array([2, 5, 1]),
...                   b=ivy.array([1, 5], [.2,.1]))
>>> y = ivy.Container.static_argsort(x,axis=-1, descending=True, stable=False)
>>> print(y)
{
    a: ivy.array([2, 0, 1]),
    b: ivy.array([[1, 0],[0,1]])
}
>>> x = ivy.Container(a=ivy.native_array([2, 5, 1]),
...                   b=ivy.array([1, 5], [.2,.1]))
>>> y = ivy.Container.static_argsort(x, axis=-1, descending=True, stable=False)
>>> print(y)
{
    a: ivy.array([2, 0, 1]),
    b: ivy.array([[1, 0],[0,1]])
}
static _static_searchsorted(x1, v, /, *, side='left', sorter=None, ret_dtype='int64', key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, out=None)[source]#

ivy.Container static method variant of ivy.searchsorted.

This method simply wraps the function, and so the docstring for ivy.searchsorted also applies to this method with minimal changes.

Return type:

Container

static _static_sort(x, /, *, axis=-1, descending=False, stable=True, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, out=None)[source]#

ivy.Container static 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.

Examples

With one ivy.Container input:

>>> x = ivy.Container(a=ivy.array([5, 9, 0.2]),
...                   b=ivy.array([[8, 1], [5, 0.8]]))
>>> y = ivy.Container.static_sort(x)
>>> 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.Container.static_sort(x)
>>> print(y)
{
    a: ivy.array([0.5, 6., 8.]),
    b: ivy.array([[0.7, 9.], [0., 0.4]])
}
Return type:

Container

argsort(*, 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 (int) – axis along which to sort. If set to -1, the function (default: -1) must sort along the last axis. Default: -1.

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

  • stable (bool) – sort stability. If True, the returned indices must (default: True) 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]]]) – The key-chains to apply or not apply the method to. Default is None. (default: None)

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

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

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

  • out (Optional[Container]) – optional output container, for writing the result to. (default: None) 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])
}
msort(*, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, out=None)[source]#

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

Parameters:
  • self (Container) – input container with array-like inputs to sort.

  • out (Optional[Container]) – optional output container, for writing the result to. (default: None)

Return type:

Container

Returns:

ret – a container containing the sorted input arrays.

Examples

>>> a = ivy.Container(x = ivy.asarray([[8, 9, 6],[6, 2, 6]]),
...                   y = ivy.asarray([[7, 2],[3, 4]])
>>> a.msort()
{
    x: ivy.array(
        [[6, 2, 6],
         [8, 9, 6]]
        ),
    y: ivy.array(
        [[3, 4],
         [7, 2]]
        )
}
searchsorted(v, /, *, side='left', sorter=None, ret_dtype='int64', key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, out=None)[source]#

ivy.Container instance method variant of ivy.searchsorted.

This method simply wraps the function, and so the docstring for ivy.searchsorted also applies to this method with minimal changes.

Return type:

Container

sort(*, 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.

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]])
}
Return type:

Container

static static_msort(a, /, *, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, out=None)[source]#

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

Parameters:
  • a (Union[Array, NativeArray, Container, list, tuple]) – array-like or container input.

  • out (Optional[Container]) – optional output container, for writing the result to. (default: None)

Return type:

Container

Returns:

ret – a container containing sorted input arrays.

Examples

With ivy.Container input:

>>> a = ivy.Container(x = ivy.asarray([[8, 9, 6],[6, 2, 6]]),
...                   y = ivy.asarray([[7, 2],[3, 4]])
>>> ivy.Container.static_lexsort(a)
{
    x: ivy.array(
        [[6, 2, 6],
         [8, 9, 6]]
        ),
    y: ivy.array(
        [[3, 4],
         [7, 2]]
        )
}