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
(
self,
variables: (
Hashable
| DataArray
| Sequence[Hashable | DataArray]
| Callable[[Self], Hashable | DataArray | list[Hashable | DataArray]]
),
ascending: bool = True,
)
| 8069 | return self._replace(variables, indexes=indexes) |
| 8070 | |
| 8071 | def sortby( |
| 8072 | self, |
| 8073 | variables: ( |
| 8074 | Hashable |
| 8075 | | DataArray |
| 8076 | | Sequence[Hashable | DataArray] |
| 8077 | | Callable[[Self], Hashable | DataArray | list[Hashable | DataArray]] |
| 8078 | ), |
| 8079 | ascending: bool = True, |
| 8080 | ) -> Self: |
| 8081 | """ |
| 8082 | Sort object by labels or values (along an axis). |
| 8083 | |
| 8084 | Sorts the dataset, either along specified dimensions, |
| 8085 | or according to values of 1-D dataarrays that share dimension |
| 8086 | with calling object. |
| 8087 | |
| 8088 | If the input variables are dataarrays, then the dataarrays are aligned |
| 8089 | (via left-join) to the calling object prior to sorting by cell values. |
| 8090 | NaNs are sorted to the end, following Numpy convention. |
| 8091 | |
| 8092 | If multiple sorts along the same dimension is |
| 8093 | given, numpy's lexsort is performed along that dimension: |
| 8094 | https://numpy.org/doc/stable/reference/generated/numpy.lexsort.html |
| 8095 | and the FIRST key in the sequence is used as the primary sort key, |
| 8096 | followed by the 2nd key, etc. |
| 8097 | |
| 8098 | Parameters |
| 8099 | ---------- |
| 8100 | variables : Hashable, DataArray, sequence of Hashable or DataArray, or Callable |
| 8101 | 1D DataArray objects or name(s) of 1D variable(s) in coords whose values are |
| 8102 | used to sort this array. If a callable, the callable is passed this object, |
| 8103 | and the result is used as the value for cond. |
| 8104 | ascending : bool, default: True |
| 8105 | Whether to sort by ascending or descending order. |
| 8106 | |
| 8107 | Returns |
| 8108 | ------- |
| 8109 | sorted : Dataset |
| 8110 | A new dataset where all the specified dims are sorted by dim |
| 8111 | labels. |
| 8112 | |
| 8113 | See Also |
| 8114 | -------- |
| 8115 | DataArray.sortby |
| 8116 | numpy.sort |
| 8117 | pandas.sort_values |
| 8118 | pandas.sort_index |
| 8119 | |
| 8120 | Examples |
| 8121 | -------- |
| 8122 | >>> ds = xr.Dataset( |
| 8123 | ... { |
| 8124 | ... "A": (("x", "y"), [[1, 2], [3, 4]]), |
| 8125 | ... "B": (("x", "y"), [[5, 6], [7, 8]]), |
| 8126 | ... }, |
| 8127 | ... coords={"x": ["b", "a"], "y": [1, 0]}, |
| 8128 | ... ) |