# xarray.DataArray.sortby

#### DataArray.sortby(variables, ascending=True)[[source]](https://github.com/pydata/xarray/blob/main/xarray/core/dataarray.py#L5353-L5434)

Sort object by labels or values (along an axis).

Sorts the dataarray, either along specified dimensions,
or according to values of 1-D dataarrays that share dimension
with calling object.

If the input variables are dataarrays, then the dataarrays are aligned
(via left-join) to the calling object prior to sorting by cell values.
NaNs are sorted to the end, following Numpy convention.

If multiple sorts along the same dimension is
given, numpy’s lexsort is performed along that dimension:
[https://numpy.org/doc/stable/reference/generated/numpy.lexsort.html](https://numpy.org/doc/stable/reference/generated/numpy.lexsort.html)
and the FIRST key in the sequence is used as the primary sort key,
followed by the 2nd key, etc.
Sorting is stable: when all sort keys compare equal, the original order is
preserved.

* **Parameters:**
  * **variables** (`Hashable`, [`DataArray`](xarray.DataArray.html.md#xarray.DataArray), [sequence](https://docs.python.org/3/glossary.html#term-sequence) of `Hashable` or [`DataArray`](xarray.DataArray.html.md#xarray.DataArray), or `Callable`) – 1D DataArray objects or name(s) of 1D variable(s) in coords whose values are
    used to sort this array. If a callable, the callable is passed this object,
    and the result is used as the value for cond.
  * **ascending** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool), *default*: [`True`](https://docs.python.org/3/builtins/constants.html#True)) – Whether to sort by ascending or descending order.
* **Returns:**
  **sorted** ([`DataArray`](xarray.DataArray.html.md#xarray.DataArray)) – A new dataarray where all the specified dims are sorted by dim
  labels.

#### SEE ALSO
[`Dataset.sortby`](xarray.Dataset.sortby.html.md#xarray.Dataset.sortby), [`numpy.sort`](https://numpy.org/doc/stable/reference/generated/numpy.sort.html#numpy.sort), `pandas.sort_values`, `pandas.sort_index`

### Examples

<div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesShowIframe('92668d3b-9681-4bd6-9646-3702aa893ef9','ef1b64f4-e281-42bd-8dd0-7364df339232','237aadbc-d956-46b4-81cf-e4fa73ddc873','../lite/tree/../notebooks/index.html?path=e8a5b314_7d0f_434a_947c_e9d25bb1c56a.ipynb','None')">Try it in your browser!</button></div>
```pycon
>>> da = xr.DataArray(
...     np.arange(5, 0, -1),
...     coords=[pd.date_range("1/1/2000", periods=5)],
...     dims="time",
... )
>>> da
<xarray.DataArray (time: 5)> Size: 40B
array([5, 4, 3, 2, 1])
Coordinates:
  * time     (time) datetime64[us] 40B 2000-01-01 2000-01-02 ... 2000-01-05
```

```pycon
>>> da.sortby(da)
<xarray.DataArray (time: 5)> Size: 40B
array([1, 2, 3, 4, 5])
Coordinates:
  * time     (time) datetime64[us] 40B 2000-01-05 2000-01-04 ... 2000-01-01
```

```pycon
>>> da.sortby(lambda x: x)
<xarray.DataArray (time: 5)> Size: 40B
array([1, 2, 3, 4, 5])
Coordinates:
  * time     (time) datetime64[us] 40B 2000-01-05 2000-01-04 ... 2000-01-01
```

<div id="237aadbc-d956-46b4-81cf-e4fa73ddc873" class="try_examples_outer_iframe  hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('92668d3b-9681-4bd6-9646-3702aa893ef9','237aadbc-d956-46b4-81cf-e4fa73ddc873')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('92668d3b-9681-4bd6-9646-3702aa893ef9','237aadbc-d956-46b4-81cf-e4fa73ddc873')">Open In Tab</button></div><div id="ef1b64f4-e281-42bd-8dd0-7364df339232" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script>
