lexsort#

ivy.lexsort(keys, /, *, axis=-1, out=None)[source]#

Perform an indirect stable sort with an array of keys in ascending order, with the last key used as primary sort order, second-to-last for secondary, and so on. Each row of the key must have the same length, which will also be the length of the returned array of integer indices, which describes the sort order.

Parameters:
  • keys (Union[Array, NativeArray]) – array-like input of size (k, N). N is the shape of each key, key can have multiple dimension.

  • axis (int, default: -1) – axis of each key to be indirectly sorted. By default, sort over the last axis of each key.

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

Return type:

Array

Returns:

ret – array of integer(type int64) indices with shape N, that sort the keys.

Examples

>>> a = [1,5,1,4,3,4,4] # First column
>>> b = [9,4,0,4,0,2,1] # Second column
>>> ivy.lexsort([b, a]) # Sort by a, then by b
array([2, 0, 4, 6, 5, 3, 1])
Array.lexsort(self, /, *, axis=-1, out=None)[source]#

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

Parameters:
  • self (Array) – input array.

  • axis (int, default: -1) – axis of each key to be indirectly sorted. By default, sort over the last axis of each key.

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

Return type:

Array

Returns:

ret – array of integer indices with shape N, that sort the input array as keys.

Examples

>>> a = [1,5,1,4,3,4,4] # First column
>>> b = [9,4,0,4,0,2,1] # Second column
>>> keys = ivy.asarray([b,a])
>>> keys.lexsort() # Sort by a, then by b
array([2, 0, 4, 6, 5, 3, 1])
Container.lexsort(self, /, *, axis=-1, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, out=None)[source]#

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

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

  • axis (Union[int, Container], default: -1) – axis of each key to be indirectly sorted. By default, sort over the last axis of each key.

  • 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([[9,4,0,4,0,2,1],[1,5,1,4,3,4,4]]),
...                   y = ivy.asarray([[1, 5, 2],[3, 4, 4]])
>>> a.lexsort()
{
    x: ivy.array([2, 0, 4, 6, 5, 3, 1])),
    y: ivy.array([0, 2, 1])
}