> ## 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`
</Note>

## Parameters

| Parameter | Description           | Type   | Required | Values    |
| :-------- | :-------------------- | :----- | :------- | :-------- |
| field     | Field name to sort by | string | ✓        | -         |
| order     | Sort order direction  | string |          | asc, desc |

### 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.

<CodeGroup>
  ```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(collection_name)
      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(collectionName);
  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(collectionName)
    results, err := coll.Query(ctx, lambdadb.QueryCollectionInput{
      Query: map[string]interface{}{"queryString": map[string]interface{}{"query": "*:*"}},
      Size:  10,
      Sort:  []map[string]interface{}{{"field1": "desc"}, {"field2": "asc"}},
    })
    if err != nil {
      log.Fatal(err)
    }
    _ = results
  }
  ```
</CodeGroup>

## 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.
</Note>
