Skip to content

sort

Returns a sorted copy of an input array x.

Parameters:

Name Type Description Default
x SparseArray

Input array. Should have a real-valued data type.

required
axis int

Axis along which to sort. If set to -1, the function must sort along the last axis. Default: -1.

-1
descending bool_

Sort order. If True, the array must be sorted in descending order (by value). If False, the array must be sorted in ascending order (by value). Default: False.

False
stable bool_

Whether the sort is stable. Only False is supported currently.

False

Returns:

Name Type Description
out COO

A sorted array.

Raises:

Type Description
ValueError

If the input array isn't and can't be converted to COO format.

Examples:

>>> import sparse
>>> x = sparse.COO.from_numpy([1, 0, 2, 0, 2, -3])
>>> sparse.sort(x).todense()
array([-3,  0,  0,  1,  2,  2])
>>> sparse.sort(x, descending=True).todense()
array([ 2,  2,  1,  0,  0, -3])
Source code in sparse/numba_backend/_coo/common.py
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
def sort(x, /, *, axis=-1, descending=False, stable=False):
    """
    Returns a sorted copy of an input array ``x``.

    Parameters
    ----------
    x : SparseArray
        Input array. Should have a real-valued data type.
    axis : int
        Axis along which to sort. If set to ``-1``, the function must sort along
        the last axis. Default: ``-1``.
    descending : bool
        Sort order. If ``True``, the array must be sorted in descending order (by value).
        If ``False``, the array must be sorted in ascending order (by value).
        Default: ``False``.
    stable : bool
        Whether the sort is stable. Only ``False`` is supported currently.

    Returns
    -------
    out : COO
        A sorted array.

    Raises
    ------
    ValueError
        If the input array isn't and can't be converted to COO format.

    Examples
    --------
    >>> import sparse
    >>> x = sparse.COO.from_numpy([1, 0, 2, 0, 2, -3])
    >>> sparse.sort(x).todense()
    array([-3,  0,  0,  1,  2,  2])
    >>> sparse.sort(x, descending=True).todense()
    array([ 2,  2,  1,  0,  0, -3])

    """
    from .._common import moveaxis
    from .core import COO

    x = _validate_coo_input(x)

    if stable:
        raise ValueError("`stable=True` isn't currently supported.")

    original_ndim = x.ndim
    if x.ndim == 1:
        x = x[None, :]
        axis = -1

    x = moveaxis(x, source=axis, destination=-1)
    x_shape = x.shape
    x = x.reshape((-1, x_shape[-1]))

    new_coords, new_data = _sort_coo(x.coords, x.data, x.fill_value, sort_axis_len=x_shape[-1], descending=descending)

    x = COO(new_coords, new_data, x.shape, has_duplicates=False, sorted=True, fill_value=x.fill_value)

    x = x.reshape(x_shape[:-1] + (x_shape[-1],))
    x = moveaxis(x, source=-1, destination=axis)
    if original_ndim == x.ndim:
        return x
    x = x.squeeze()
    if x.shape == ():
        return x[None]
    return x