---
title: "Indexes: Sorting & Collation"
description: "Configure custom sorting and collation in RavenDB indexes to control result ordering for different languages and data types."
url: "https://docs.ravendb.net/7.2/indexes/sorting-and-collation"
version: "7.2"
language: "csharp"
---

# Indexes: Sorting & Collation

> Client language: C#. Also available for [Java](https://docs.ravendb.net/7.2/indexes/sorting-and-collation.java.md).
>
> RavenDB publishes an official agent skill for coding assistants at https://docs.ravendb.net/skills/ravendb/SKILL.md (also installable with `npx skills add ravendb/skills`). The skill describes itself as: "Read this before writing any RavenDB query, client call or /databases/ request. Covers server setup, document modelling, the Node.js, .NET and Python clients, RQL, indexes, vector search and AI agents. Code written from recall tends to run and return the wrong rows, because RQL resembles SQL and the clients take field names as plain strings."

Starting from version 4.0, RavenDB automatically determines sorting based on an indexed value. All values will have a capability to be sorted `lexicographically`. Numerical values will also be sortable by their `double` and `long` value.

## Date types

Dates are written to the index in a form which preserves lexicography order, and is readable by both human and machine (like so: `2011-04-04T11:28:46.0404749+03:00`). This requires no user intervention.

## Example

Please read our dedicated article describing `sorting` capabilities when queries are executed. It can be found [here](https://docs.ravendb.net/7.2/querying/sorting-query-results/sort-query-results.md).

## Collation

RavenDB supports using collations for documents sorting and indexing. You can setup a specific collation for an index field, so you can sort based of culture specific rules.

The following is an example of an index definition which allows sorting based on the Swedish lexical sorting rules:

### LINQ

```csharp
public class Products_ByName : AbstractIndexCreationTask<Product>
{
  public Products_ByName()
  {
      Map = products => from product in products
                        select new
                        {
                            product.Name
                        };

      Analyzers.Add(x => x.Name, "Raven.Server.Documents.Indexes.Persistence.Lucene.Analyzers.Collation.Cultures.SvCollationAnalyzer, Raven.Server");
  }
}
```

### JavaScript

```csharp
private class Products_ByName : AbstractJavaScriptIndexCreationTask
{
  public Products_ByName()
  {
      Maps = new HashSet<string>
      {
          @"map('products', function (u){
                          return {
                              Name: u.Name,
                              _: {$value: u.Name, $name:'AnalyzedName'}
                          };
                      })",
      };
      Fields = new Dictionary<string, IndexFieldOptions>
      {
          {
              "AnalyzedName", new IndexFieldOptions()
              {
                  Indexing = FieldIndexing.Search,
                  Analyzer = "StandardAnalyzer"
              }
          }
      };
  }
  public class Result
  {
      public string AnalyzedName { get; set; }
  }
}
```

In general, you can sort using `Raven.Database.Indexing.Collation.Cultures.<two-letters-culture-name>CollationAnalyzer`, and *all* the cultures supported by the .NET framework are supported.

## See also

- [Map Indexes](https://docs.ravendb.net/7.2/indexes/map-indexes.md)
- [What are Indexes](https://docs.ravendb.net/7.2/indexes/what-are-indexes.md)
- [Query Overview](https://docs.ravendb.net/7.2/querying/overview.md)
- [Sorting](https://docs.ravendb.net/7.2/querying/sorting-query-results/sort-query-results.md)
- [Basics](https://docs.ravendb.net/7.2/indexes/querying/query-index.md)
