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, build_callable=False, **kwargs)[source]#
Bases:
ContainerBase
- _abc_impl = <_abc._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 (
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. IfTrue
, the returned indices sortx
in descending order (by value). IfFalse
, the returned indices sortx
in ascending order (by value). Default:False
.stable (
Union
[bool
,Container
], default:True
) – sort stability. IfTrue
, 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
.key_chains (
Optional
[Union
[List
[str
],Dict
[str
,str
],Container
]], default:None
) – The key-chains to apply or not apply the method to. Default isNone
.to_apply (
Union
[bool
,Container
], default:True
) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default isTrue
.prune_unapplied (
Union
[bool
,Container
], default:False
) – Whether to prune key_chains for which the function was not applied. Default isFalse
.map_sequences (
Union
[bool
,Container
], default:False
) – Whether to also map method to sequences (lists, tuples). Default isFalse
.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
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.
- Return type:
Container
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]]) }
- 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 (
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. IfTrue
, the returned indices sortx
in descending order (by value). IfFalse
, the returned indices sortx
in ascending order (by value). Default:False
.stable (
Union
[bool
,Container
], default:True
) – sort stability. IfTrue
, 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
.key_chains (
Optional
[Union
[List
[str
],Dict
[str
,str
],Container
]], default:None
) – The key-chains to apply or not apply the method to. Default isNone
.to_apply (
Union
[bool
,Container
], default:True
) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default isTrue
.prune_unapplied (
Union
[bool
,Container
], default:False
) – Whether to prune key_chains for which the function was not applied. Default isFalse
.map_sequences (
Union
[bool
,Container
], default:False
) – Whether to also map method to sequences (lists, tuples). Default isFalse
.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]) }
- 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
], default:None
) – optional output container, for writing the result to.
- 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.
- 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]]) }
- 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
], default:None
) – optional output container, for writing the result to.
- 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]] ) }
This should have hopefully given you an overview of the sorting submodule, if you have any questions, please feel free to reach out on our discord!