# xarray.Dataset.sortby

#### Dataset.sortby(variables, ascending=True)[[source]](https://github.com/pydata/xarray/blob/main/xarray/core/dataset.py#L8263-L8376)

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

Sorts the dataset, 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** ([`Dataset`](xarray.Dataset.html.md#xarray.Dataset)) – A new dataset where all the specified dims are sorted by dim
  labels.

#### SEE ALSO
[`DataArray.sortby`](xarray.DataArray.sortby.html.md#xarray.DataArray.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('9bc9e1bd-4e55-4322-a9f8-69e38fe36b7e','f2d0ee9a-77de-41be-9df8-2e1117dd2c54','33793d54-ffb2-4db4-8965-30f62051c0e9','../lite/tree/../notebooks/index.html?path=d0cf1a62_95d0_4a93_8e26_414b2648cb87.ipynb','None')">Try it in your browser!</button></div>
```pycon
>>> ds = xr.Dataset(
...     {
...         "A": (("x", "y"), [[1, 2], [3, 4]]),
...         "B": (("x", "y"), [[5, 6], [7, 8]]),
...     },
...     coords={"x": ["b", "a"], "y": [1, 0]},
... )
>>> ds.sortby("x")
<xarray.Dataset> Size: 88B
Dimensions:  (x: 2, y: 2)
Coordinates:
  * x        (x) <U1 8B 'a' 'b'
  * y        (y) int64 16B 1 0
Data variables:
    A        (x, y) int64 32B 3 4 1 2
    B        (x, y) int64 32B 7 8 5 6
>>> ds.sortby(lambda x: -x["y"])
<xarray.Dataset> Size: 88B
Dimensions:  (x: 2, y: 2)
Coordinates:
  * x        (x) <U1 8B 'b' 'a'
  * y        (y) int64 16B 1 0
Data variables:
    A        (x, y) int64 32B 1 2 3 4
    B        (x, y) int64 32B 5 6 7 8
```

<div id="33793d54-ffb2-4db4-8965-30f62051c0e9" class="try_examples_outer_iframe  hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('9bc9e1bd-4e55-4322-a9f8-69e38fe36b7e','33793d54-ffb2-4db4-8965-30f62051c0e9')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('9bc9e1bd-4e55-4322-a9f8-69e38fe36b7e','33793d54-ffb2-4db4-8965-30f62051c0e9')">Open In Tab</button></div><div id="f2d0ee9a-77de-41be-9df8-2e1117dd2c54" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script>
