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

# Sort search results

> Sort LambdaDB query results by one or more fields in ascending or descending order. Supports long, double, datetime, and keyword field types.

LambdaDB allows you to add one or more sorts on specific fields. Each sort can be reversed as well. The sort is defined on a per field level.

> [!NOTE]
>
> Sorting can be done on the following data types:
>
> * `long`
> * `double`
> * `datetime`
> * `keyword`

## Request format

Set the request's `sort` to an array of objects mapping indexed field names to `"asc"` or `"desc"`, such as `[{"timestamp": "desc"}]`. The field name is the object key and the direction is its value; there are no literal `field` or `order` properties.

Omitting `sort` or setting it to `null` means no explicit sort. [Native reranking](https://docs.lambdadb.ai/guides/search/native-reranking.md) rejects any non-null `sort`, including an empty array.

### Sort order options

| Value | Description |
| :- | :- |
| asc | Sorts in ascending order |
| desc | Sorts in descending order |

## Usage

The sort parameter accepts an array of sort objects, allowing multiple field sorting with different orders.

```python Python theme={null}
from lambdadb import LambdaDB

with LambdaDB(
    project_api_key="YOUR_API_KEY",
    base_url="YOUR_BASE_URL",
    project_name="YOUR_PROJECT_NAME",
) as client:
    coll = client.collection("my_collection")
    results = coll.query(
        query={"queryString": {"query": "*:*"}},
        size=10,
        sort=[{"field1": "desc"}, {"field2": "asc"}],
    )
```

```typescript TypeScript theme={null}
import { LambdaDBClient } from "@functional-systems/lambdadb";

const client = new LambdaDBClient({
  projectApiKey: "YOUR_API_KEY",
  baseUrl: "YOUR_BASE_URL",
  projectName: "YOUR_PROJECT_NAME",
});
const collection = client.collection("my_collection");
const results = await collection.query({
  query: { queryString: { query: "*:*" } },
  size: 10,
  sort: [{ field1: "desc" }, { field2: "asc" }],
});
```

```go Go theme={null}
package main

import (
  "context"
  "log"
  lambdadb "github.com/lambdadb/go-lambdadb"
)

func main() {
  ctx := context.Background()
  client := lambdadb.New(
    lambdadb.WithBaseURL("YOUR_BASE_URL"),
    lambdadb.WithProjectName("YOUR_PROJECT_NAME"),
    lambdadb.WithAPIKey("YOUR_API_KEY"),
  )
  coll := client.Collection("my_collection")
  results, err := coll.Query(ctx, lambdadb.QueryInput{
    Query: map[string]interface{}{"queryString": map[string]interface{}{"query": "*:*"}},
    Size:  lambdadb.Int64(10),
    Sort:  []map[string]interface{}{{"field1": "desc"}, {"field2": "asc"}},
  })
  if err != nil {
    log.Fatal(err)
  }
  _ = results
}
```

## Examples

### Single field sort

Sort by timestamp in descending order (newest first):

```json theme={null}
[{"timestamp": "desc"}]
```

### Multiple field sort

Sort by priority (descending) first, then by created\_at (ascending):

```json theme={null}
[{"priority": "desc"}, {"created_at": "asc"}]
```

> [!NOTE]
>
> When multiple sort fields are specified, the sorting is applied in the order they appear in the array. Documents are first sorted by the first field, then by the second field for documents with the same first field value, and so on.

## Keyword array sorting

For `keyword` arrays, ascending order selects the smallest indexed value and descending order selects the largest. For example, `["a", "z"]` sorts using `"a"` in ascending order and `"z"` in descending order. Single values follow the same ordering. Values use Unicode code point order, without analysis, locale collation, or case folding.

Documents with no indexed keyword value sort last in ascending order and first in descending order. Missing fields, null fields, empty arrays, and values excluded by the keyword length limit have no indexed sort value. Empty strings are indexed values.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.