@@ -5218,6 +5218,18 @@ def indices(self, order: str | list[str] | None = None, **kwargs: Any) -> NDArra
52185218 """
52195219 return indices (self , order , ** kwargs )
52205220
5221+ def argsort (self , order : str | list [str ] | None = None , ** kwargs : Any ) -> NDArray :
5222+ """
5223+ Return the permutation that sorts the array.
5224+
5225+ This follows :func:`numpy.argsort` semantics more closely than
5226+ :meth:`indices`: plain 1-D arrays are supported, and ``order=None``
5227+ means "use the array's natural order" rather than "leave unsorted".
5228+
5229+ See full documentation in :func:`argsort`.
5230+ """
5231+ return argsort (self , order , ** kwargs )
5232+
52215233 def itersorted (
52225234 self ,
52235235 order : str | list [str ] | None = None ,
@@ -6748,6 +6760,60 @@ def indices(array: blosc2.Array, order: str | list[str] | None = None, **kwargs:
67486760 return larr .indices (order ).compute (** kwargs )
67496761
67506762
6763+ def argsort (array : blosc2 .Array , order : str | list [str ] | None = None , ** kwargs : Any ) -> NDArray :
6764+ """
6765+ Return the indices that would sort the array.
6766+
6767+ This mirrors :func:`numpy.argsort` for 1-D arrays. Plain arrays sort by
6768+ their values. Structured arrays sort by ``order`` when provided, or by
6769+ their dtype field order when ``order=None``. Expression orders such as
6770+ ``"abs(x)"`` are also supported when a matching ``full`` expression index
6771+ exists.
6772+
6773+ Parameters
6774+ ----------
6775+ array: :ref:`blosc2.Array`
6776+ The 1-D array to be ordered.
6777+ order: str, list of str, optional
6778+ Primary and optional secondary order keys for structured arrays. When
6779+ omitted, NumPy's default record order is used for structured dtypes and
6780+ the array values themselves are used for plain dtypes.
6781+ kwargs: Any, optional
6782+ Keyword arguments that are supported by the :func:`empty` constructor.
6783+
6784+ Returns
6785+ -------
6786+ out: :ref:`NDArray`
6787+ The ordered logical positions as ``int64``.
6788+
6789+ Notes
6790+ -----
6791+ When the primary order key has a matching ``full`` field or expression
6792+ index, the permutation is returned directly from that index in ascending
6793+ stable order. Secondary keys refine ties after the primary indexed order.
6794+ Without a matching ``full`` index, :func:`argsort` falls back to
6795+ materializing the input values and delegating ordering to
6796+ :func:`numpy.argsort`.
6797+
6798+ The result is always a new array materialization. For persistent inputs,
6799+ the returned permutation is in memory by default; pass storage kwargs such
6800+ as ``urlpath`` (and typically ``mode="w"``) if the permutation should also
6801+ be persisted on disk.
6802+ """
6803+ if isinstance (array , blosc2 .NDArray ):
6804+ from . import indexing
6805+
6806+ ordered = indexing .ordered_indices (array , order = order )
6807+ if ordered is not None :
6808+ return blosc2 .asarray (ordered , ** kwargs )
6809+ if indexing .is_expression_order (array , order ):
6810+ raise ValueError ("expression order requires a matching full expression index" )
6811+
6812+ values = array [:]
6813+ positions = np .argsort (values , order = order , kind = "stable" )
6814+ return blosc2 .asarray (positions .astype (np .int64 , copy = False ), ** kwargs )
6815+
6816+
67516817def sort (array : blosc2 .Array , order : str | list [str ] | None = None , ** kwargs : Any ) -> NDArray :
67526818 """
67536819 Return a sorted array following the specified order.
0 commit comments