
## Sort search results

URL: https://docs.atlan.com/product/capabilities/build-apps/sdks/python/search/references/sort

> Sort search results programmatically using the Atlan Python SDK (pyatlan) by one or multiple properties with Elasticsearch DSL.

# Sort search results

You can sort search results by one or multiple properties.

:::warning[Limited to iterating through less than 100,000 results]
You can sort any number of results; however, if you plan to [page](https://docs.atlan.com/llms/platform/python/paging/llms.txt) through more than 100,000 total results you can't specify any sort order.
:::

### Java

```java showLineNumbers title="Annotated sort options, as you would define them in the Java SDK"
SortOptions byUpdate = Asset.UPDATE_TIME.order(SortOrder.Desc); // (1)
SortOptions byCreation = Asset.CREATE_TIME.order(SortOrder.Asc); // (2)
```

1. For each property you can specify whether to sort the results in `desc`ending order or `asc`ending order. In this example our first results would be the ones changed most recently.

 ```java title="Equivalent sort option from Elastic"
 SortOptions byUpdate = SortOptions.of(s -> s
 .field(FieldSort.of(f -> f // (1)
 .field("__modificationTimestamp")
 .order(SortOrder.Desc))));
 ```

2. In this example, our first results would be the oldest ones (based on when they were created).

 ```java title="Equivalent sort option from Elastic"
 SortOptions byCreation = SortOptions.of(s -> s
 .field(FieldSort.of(f -> f // (2)
 .field("__timestamp")
 .order(SortOrder.Asc))));
 ```

```java title="Build the request"
IndexSearchRequest index = client.assets.select()
 .where(...) // (1)
 .sort(byUpdate) // (2)
 .sort(byCreation) // (3)
 .toRequest();
```

1. You still need a query, to get some results to sort.
2. Note that the sort itself is an array—hence we can add multiple sort options and they'll be evaluated in order (to sort by multiple properties). In this example our first results would be the ones changed most recently.
3. For any results that have identical modification dates, we would then sort within those results to present the oldest (created) results first.

```java title="Run the search"
IndexSearchResponse response = index.search(client);
for (Asset result : response)
```

### Python

```python showLineNumbers title="Annotated sort options, as you would define them in the Python SDK"
from pyatlan.client.atlan import AtlanClient
from pyatlan.model.enums import SortOrder
from pyatlan.model.assets import Referenceable
from pyatlan.model.search import IndexSearchRequest, DSL

by_update = Referenceable.UPDATE_TIME.order(SortOrder.DESCENDING) # (1)
by_creation = Referenceable.CREATE_TIME.order(SortOrder.ASCENDING) # (2)
```

1. For each property you can specify whether to sort the results in descending order or ascending order. In this example our first results would be the ones changed most recently.
2. In this example, our first results would be the oldest ones (based on when they were created).

```python title="Build the request"
index = (
 FluentSearch()
 .where(...) # (1)
 .sort(by_update) # (2)
 .sort(by_creation) # (3)
).to_request()
```

1. You still need a query, to get some results to sort.
2. Note that the sort itself is an array—hence we can add multiple sort options and they'll be evaluated in order (to sort by multiple properties). In this example our first results would be the ones changed most recently.
3. For any results that have identical modification dates, we would then sort within those results to present the oldest (created) results first.

```python title="Run the search"
client = AtlanClient();
response = client.asset.search(index)
for result in response:
 # Do something with the ordered results...
```

### Kotlin

```kotlin showLineNumbers title="Annotated sort options, as you would define them in the Java SDK"
val byUpdate = Asset.UPDATE_TIME.order(SortOrder.Desc) // (1)
val byCreation = Asset.CREATE_TIME.order(SortOrder.Asc) // (2)
```

1. For each property you can specify whether to sort the results in `desc`ending order or `asc`ending order. In this example our first results would be the ones changed most recently.

 ```kotlin title="Equivalent sort option from Elastic"
 val byUpdate = SortOptions.of(s -> s
 .field(FieldSort.of(f -> f // (1)
 .field("__modificationTimestamp")
 .order(SortOrder.Desc))))
 ```

2. In this example, our first results would be the oldest ones (based on when they were created).

 ```kotlin title="Equivalent sort option from Elastic"
 val byCreation = SortOptions.of(s -> s
 .field(FieldSort.of(f -> f // (2)
 .field("__timestamp")
 .order(SortOrder.Asc))))
 ```

```kotlin title="Build the request"
val index = client.assets.select()
 .where(...) // (1)
 .sort(byUpdate) // (2)
 .sort(byCreation) // (3)
 .toRequest()
```

1. You still need a query, to get some results to sort.
2. Note that the sort itself is an array—hence we can add multiple sort options and they'll be evaluated in order (to sort by multiple properties). In this example our first results would be the ones changed most recently.
3. For any results that have identical modification dates, we would then sort within those results to present the oldest (created) results first.

```kotlin title="Run the search"
val response = index.search(client)
for (result in response)
```

### Raw REST API

```json showLineNumbers title="POST /api/meta/search/indexsearch"
{
 "dsl": {
 "query": {...}, // (1)
 "sort": [ // (2)}, // (3)} // (4)
 ]
 }
}
```

1. You still need a query, to get some results to sort.
2. Note that the sort itself is an array—hence you can sort by multiple properties.
3. For each property you can specify whether to sort the results in `desc`ending order or `asc`ending order. In this example our first results would be the ones changed most recently.
4. For any results that have identical modification dates, we would then sort within those results to present the oldest (created) results first.

:::tip[See Elastic's documentation for more details]
The examples above should cover most scenarios. If you want to get fancy, see [Elastic's documentation on sorting](https://www.elastic.co/guide/en/elasticsearch/reference/current/sort-search-results.html).
:::

---
