> ## Documentation Index
> Fetch the complete documentation index at: https://docs.activeviam.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Ordering

<span id="atoti.Ordering" />

> atoti.Ordering(<br />
>     \*,<br />
>     *direction*: [Literal](https://docs.python.org/3/library/typing.html#typing.Literal)\['ascending', 'descending'] = `'ascending'`,<br />
>     *first*: [Sequence](https://docs.python.org/3/library/collections.abc.html#collections.abc.Sequence)\[[bool](https://docs.python.org/3/library/functions.html#bool) | [int](https://docs.python.org/3/library/functions.html#int) | [float](https://docs.python.org/3/library/functions.html#float) | [date](https://docs.python.org/3/library/datetime.html#datetime.date) | [datetime](https://docs.python.org/3/library/datetime.html#datetime.datetime) | [time](https://docs.python.org/3/library/datetime.html#datetime.time) | [str](https://docs.python.org/3/library/stdtypes.html#str)] = `()`,<br />
>     *string\_comparison*: [StringComparison](./atoti.ordering.StringComparison#atoti.StringComparison) = `StringComparison(case='sensitive', digits='lexicographic')`,<br />
> )

The order in which elements are sorted.

```pycon theme={"languages":{"custom":["/engine/python-sdk/6.2/languages/pycon.tmLanguage.json"]}}
>>> df = pd.DataFrame({"City": ["Paris", "Berlin", "London", "Amsterdam"]})
>>> table = session.read_pandas(df, keys={"City"}, table_name="Example")
>>> cube = session.create_cube(table)
>>> l, m = cube.levels, cube.measures
```

Ascending direction:

```pycon theme={"languages":{"custom":["/engine/python-sdk/6.2/languages/pycon.tmLanguage.json"]}}
>>> l["City"].ordering = tt.Ordering(direction="ascending")
>>> cube.query(m["contributors.COUNT"], levels=[l["City"]]).index.tolist()
['Amsterdam', 'Berlin', 'London', 'Paris']
```

Descending direction:

```pycon theme={"languages":{"custom":["/engine/python-sdk/6.2/languages/pycon.tmLanguage.json"]}}
>>> l["City"].ordering = tt.Ordering(direction="descending")
>>> cube.query(m["contributors.COUNT"], levels=[l["City"]]).index.tolist()
['Paris', 'London', 'Berlin', 'Amsterdam']
```

Forcing some elements first:

```pycon theme={"languages":{"custom":["/engine/python-sdk/6.2/languages/pycon.tmLanguage.json"]}}
>>> l["City"].ordering = tt.Ordering(first=["Paris", "London"])
>>> cube.query(m["contributors.COUNT"], levels=[l["City"]]).index.tolist()
['Paris', 'London', 'Amsterdam', 'Berlin']
```

[`first`](./atoti.function.first#atoti.first) cannot be combined with the other attributes yet:

```pycon theme={"languages":{"custom":["/engine/python-sdk/6.2/languages/pycon.tmLanguage.json"]}}
>>> tt.Ordering(direction="descending", first=["Paris"])
Traceback (most recent call last):
    ...
NotImplementedError: `first` cannot be combined with a non default `direction` or `string_comparison` yet.
```

<Callout icon="link">
  **See also**:
  [`atoti.Level.ordering`](./atoti.Level.ordering#atoti.Level.ordering).
</Callout>

<Info>
  Added in version 6.2.1.
</Info>

### Attributes

<h4 id="atoti.Ordering.direction">
  *direction*
</h4>

Direction in which the elements are sorted.

* `"ascending"`: from the smallest to the greatest element.
* `"descending"`: from the greatest to the smallest element.

<h4 id="atoti.Ordering.first">
  *first*
</h4>

Elements to always sort first, in the given order.

<h4 id="atoti.Ordering.string_comparison">
  *string\_comparison*
</h4>

> [StringComparison](./atoti.ordering.StringComparison#atoti.StringComparison)
