---
layout: article
title: Order
description: Understand how to do data ordering in Appwrite DocumentsDB. Learn how to order and sort your database records for efficient data retrieval.
---

You can order results returned by Appwrite DocumentsDB by using an order query.
For best performance, create an [index](/docs/products/databases/documentsdb/collections#indexes) on the field you plan to order by.

# Ordering one field

When querying using the [listDocuments](/docs/products/databases/documentsdb/documents#list-documents) endpoint,
you can specify the order of the documents returned using the `Query.orderAsc()` and `Query.orderDesc()` query methods.

```client-web
import { Client, Query, DocumentsDB } from "appwrite";

const client = new Client()
    .setEndpoint('https://<REGION>.cloud.appwrite.io/v1')
    .setProject('<PROJECT_ID>');

const documentsDB = new DocumentsDB(client);

documentsDB.listDocuments({
    databaseId: '<DATABASE_ID>',
    collectionId: '<COLLECTION_ID>',
    queries: [
        Query.orderAsc('title'),
    ]
});
```

```client-flutter
import 'package:appwrite/appwrite.dart';

void main() async {
    final client = Client()
        .setEndpoint('https://<REGION>.cloud.appwrite.io/v1')
        .setProject('<PROJECT_ID>');

    final documentsDB = DocumentsDB(client);

    try {
        final documents = await documentsDB.listDocuments(
            databaseId: '<DATABASE_ID>',
            collectionId: '<COLLECTION_ID>',
            queries: [
                Query.orderAsc('title')
            ]
        );
    } on AppwriteException catch(e) {
        print(e);
    }
}
```

```client-apple
import Appwrite
import AppwriteModels

func main() async throws {
    let client = Client()
        .setEndpoint('https://<REGION>.cloud.appwrite.io/v1')
        .setProject('<PROJECT_ID>');

    let documentsDB = DocumentsDB(client)

    do {
        let documents = try await documentsDB.listDocuments(
            databaseId: "<DATABASE_ID>",
            collectionId: "<COLLECTION_ID>",
            queries: [
                Query.orderAsc("title")
            ]
        )
    } catch {
        print(error.localizedDescription)
    }
}
```
```client-android-kotlin
import io.appwrite.Client
import io.appwrite.Query
import io.appwrite.services.DocumentsDB

suspend fun main() {
    val client = Client(applicationContext)
        .setEndpoint('https://<REGION>.cloud.appwrite.io/v1')
        .setProject('<PROJECT_ID>');

    val documentsDB = DocumentsDB(client)

    try {
        val documents = documentsDB.listDocuments(
            databaseId = "<DATABASE_ID>",
            collectionId = "<COLLECTION_ID>",
            queries = [
                Query.orderAsc("title")
            ]
        )
    } catch (e: AppwriteException) {
        Log.e("Appwrite", e.message)
    }
}
```

```graphql
query {
    documentsDBListDocuments(
        databaseId: "<DATABASE_ID>",
        collectionId: "<COLLECTION_ID>"
        queries: ["orderAsc(\"title\")"]
    ) {
        total
        documents {
            _id
            data
        }
    }
}
```

# Multiple fields
To sort based on multiple fields, simply provide multiple query methods.
For better performance, create an index on the first field that you order by.

In the example below, the movies returned will be first sorted by `title` in ascending order, then sorted by `year` in descending order.

```js
// Web SDK code example for sorting based on multiple fields
// ...

// List documents and sort based on multiple fields
documentsDB.listDocuments({
    databaseId: '<DATABASE_ID>',
    collectionId: '<COLLECTION_ID>',
    queries: [
        Query.orderAsc('title'), // Order first by title in ascending order
        Query.orderDesc('year'), // Then, order by year in descending order
    ]
});
```
```dart
// Flutter SDK code example for sorting based on multiple fields
// ...

// List documents and sort based on multiple fields
try {
    final documents = await documentsDB.listDocuments(
        databaseId: '<DATABASE_ID>',
        collectionId: '<COLLECTION_ID>',
        queries: [
            Query.orderAsc('title'), // Order by title in ascending order
            Query.orderDesc('year')  // Order by year in descending order
        ]
    );
} on AppwriteException catch(e) {
    print(e);
}
```
```kotlin
// Android SDK code example for sorting based on multiple fields
// ...

// List documents and sort based on multiple fields
try {
    val documents = documentsDB.listDocuments(
        databaseId = "<DATABASE_ID>",
        collectionId = "<COLLECTION_ID>",
        queries = [
            Query.orderAsc("title"), // Order by title in ascending order
            Query.orderDesc("year")  // Order by year in descending order
        ]
    );
} catch (e: AppwriteException) {
    Log.e("Appwrite", e.message);
}
```
```swift
// Apple SDK code example for sorting based on multiple fields
// ...

// List documents and sort based on multiple fields
do {
    let documents = try await documentsDB.listDocuments(
        databaseId: "<DATABASE_ID>",
        collectionId: "<COLLECTION_ID>",
        queries: [
            Query.orderAsc("title"), // Order by title in ascending order
            Query.orderDesc("year")  // Order by year in descending order
        ]
    );
} catch {
    print(error.localizedDescription);
}
```
```graphql
query {
    documentsDBListDocuments(
        databaseId: "<DATABASE_ID>",
        collectionId: "<COLLECTION_ID>",
        queries: ["orderAsc(\"title\")", "orderDesc(\"year\")"]
    ) {
        total
        documents {
            _id
            data
        }
    }
}
```

# Ordering by sequence

For ordering based on insertion order, you can use the `$sequence` field, which Appwrite automatically adds to all documents. Sorting by `$sequence` returns documents in the order they were created.

```client-web
import { Client, Query, DocumentsDB } from "appwrite";

const client = new Client()
    .setEndpoint('https://<REGION>.cloud.appwrite.io/v1')
    .setProject('<PROJECT_ID>');

const documentsDB = new DocumentsDB(client);

documentsDB.listDocuments({
    databaseId: '<DATABASE_ID>',
    collectionId: '<COLLECTION_ID>',
    queries: [
        Query.orderAsc('$sequence'),
    ]
});
```

```client-flutter
import 'package:appwrite/appwrite.dart';

void main() async {
    final client = Client()
        .setEndpoint('https://<REGION>.cloud.appwrite.io/v1')
        .setProject('<PROJECT_ID>');

    final documentsDB = DocumentsDB(client);

    try {
        final documents = await documentsDB.listDocuments(
            databaseId: '<DATABASE_ID>',
            collectionId: '<COLLECTION_ID>',
            queries: [
                Query.orderAsc('\$sequence')
            ]
        );
    } on AppwriteException catch(e) {
        print(e);
    }
}
```

```client-apple
import Appwrite
import AppwriteModels

func main() async throws {
    let client = Client()
        .setEndpoint("https://<REGION>.cloud.appwrite.io/v1")
        .setProject("<PROJECT_ID>")

    let documentsDB = DocumentsDB(client)

    do {
        let documents = try await documentsDB.listDocuments(
            databaseId: "<DATABASE_ID>",
            collectionId: "<COLLECTION_ID>",
            queries: [
                Query.orderAsc("$sequence")
            ]
        )
    } catch {
        print(error.localizedDescription)
    }
}
```

```client-android-kotlin
import io.appwrite.Client
import io.appwrite.Query
import io.appwrite.services.DocumentsDB

suspend fun main() {
    val client = Client(applicationContext)
        .setEndpoint("https://<REGION>.cloud.appwrite.io/v1")
        .setProject("<PROJECT_ID>")

    val documentsDB = DocumentsDB(client)

    try {
        val documents = documentsDB.listDocuments(
            databaseId = "<DATABASE_ID>",
            collectionId = "<COLLECTION_ID>",
            queries = listOf(
                Query.orderAsc("\$sequence")
            )
        )
    } catch (e: AppwriteException) {
        Log.e("Appwrite", e.message)
    }
}
```

```graphql
query {
    documentsDBListDocuments(
        databaseId: "<DATABASE_ID>",
        collectionId: "<COLLECTION_ID>"
        queries: ["orderAsc(\"$sequence\")"]
    ) {
        total
        documents {
            _id
            data
        }
    }
}
```

The `$sequence` field is useful when you need:
- Consistent ordering for pagination, especially with high-frequency inserts
- Reliable insertion order tracking when timestamps might not be precise enough
- Simple insertion-order sorting without managing custom counter fields
