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

# Sort by attribute

> Learn how to sort an index by an attribute.

By default, Algolia puts the most relevant items first in results.
However, you might want to sort results by a specific numeric or boolean attribute, like price.

To sort your search results by a specific attribute:

1. [Create a replica index](/doc/guides/managing-results/refine-results/sorting/how-to/creating-replicas)
2. Configure an attribute for sorting
3. Update your user interface

## Replica indices

Think of a replica index as a duplicate of your primary index but with a different sorting of results.
Choose from two types of replicas: [standard or virtual](/doc/guides/managing-results/refine-results/sorting/in-depth/replicas#standard-and-virtual-replicas):

* Use a standard replica for exhaustive sorting.
  This method sorts results by the chosen attribute.
* Use a virtual replica for relevant sorting.
  This method prioritizes relevant results while still incorporating the sorting preference for the chosen attribute.

## Configure an attribute for sorting in the Algolia dashboard

Sort indices by numeric or boolean attributes.

1. [Create a replica index](/doc/guides/managing-results/refine-results/sorting/how-to/creating-replicas#create-replica-indices-in-the-dashboard).
2. Refresh the dashboard page in your browser.
3. Select the replica index.
4. On the **Configuration** tab, go to **Relevant sort** or **Ranking and Sorting**.
   You'll see either option, depending on the type of replica you want to configure (virtual or standard).
5. Click **+Add a sort attribute** or **+Add sort-by attribute**
6. Type in the name of the attribute you want to sort.
   Be careful here: Algolia doesn't check to see if what you type matches the name of an attribute in the index.
7. Determine the sort direction (**Ascending** or **Descending**).
8. Review and save your changes.

<Note>
  To rank records alphabetically by a string attribute, use [`customRanking`](/doc/api-reference/api-parameters/customRanking).
  For longer strings, precompute a numeric ranking value or add more custom ranking attributes as tie-breakers.
  For more information, see [String and null or absent values](/doc/guides/managing-results/must-do/custom-ranking/how-to/configure-custom-ranking#string-and-null-or-absent-values).
</Note>

## Configure an attribute for sorting with the API

1. [Create a replica index](/doc/guides/managing-results/refine-results/sorting/how-to/creating-replicas#create-replica-indices-with-the-api).
2. Update the [`ranking`](/doc/api-reference/api-parameters/ranking) parameter of the replica index with the [`setSettings`](/doc/libraries/sdk/v1/methods/set-settings) method.

## Update user interface

To let users select between different rankings in your user interface,
you also need to update it—for example, by providing a [`sort-by`](/doc/api-reference/widgets/sort-by/js) widget.

## Example: relevant sort by price

To run the code examples on this page, [install the latest API client](/doc/libraries/sdk/install).

This example applies relevant sorting to a virtual replica.

<CodeGroup>
  ```cs C# theme={"system"}
  var response = await client.SetSettingsAsync(
    "INDEX_NAME",
    new IndexSettings { CustomRanking = new List<string> { "desc(price)" } }
  );
  ```

  ```dart Dart theme={"system"}
  final response = await client.setSettings(
    indexName: "INDEX_NAME",
    indexSettings: IndexSettings(
      customRanking: [
        "desc(price)",
      ],
    ),
  );
  ```

  ```go Go theme={"system"}
  response, err := client.SetSettings(client.NewApiSetSettingsRequest(
    "INDEX_NAME",
    search.NewEmptyIndexSettings().SetCustomRanking(
      []string{"desc(price)"})))
  if err != nil {
    // handle the eventual error
    panic(err)
  }
  ```

  ```java Java theme={"system"}
  UpdatedAtResponse response = client.setSettings(
    "INDEX_NAME",
    new IndexSettings().setCustomRanking(Arrays.asList("desc(price)"))
  );
  ```

  ```js JavaScript theme={"system"}
  const response = await client.setSettings({
    indexName: 'theIndexName',
    indexSettings: { customRanking: ['desc(price)'] },
  });
  ```

  ```kotlin Kotlin theme={"system"}
  var response =
    client.setSettings(
      indexName = "INDEX_NAME",
      indexSettings = IndexSettings(customRanking = listOf("desc(price)")),
    )
  ```

  ```php PHP theme={"system"}
  $response = $client->setSettings(
      'INDEX_NAME',
      ['customRanking' => [
          'desc(price)',
      ],
      ],
  );
  ```

  ```python Python theme={"system"}
  response = client.set_settings(
      index_name="INDEX_NAME",
      index_settings={
          "customRanking": [
              "desc(price)",
          ],
      },
  )
  ```

  ```ruby Ruby theme={"system"}
  response = client.set_settings(
    "INDEX_NAME",
    Algolia::Search::IndexSettings.new(custom_ranking: ["desc(price)"])
  )
  ```

  ```scala Scala theme={"system"}
  val response = Await.result(
    client.setSettings(
      indexName = "INDEX_NAME",
      indexSettings = IndexSettings(
        customRanking = Some(Seq("desc(price)"))
      )
    ),
    Duration(100, "sec")
  )
  ```

  ```swift Swift theme={"system"}
  let response = try await client.setSettings(
      indexName: "INDEX_NAME",
      indexSettings: IndexSettings(customRanking: ["desc(price)"])
  )
  ```
</CodeGroup>

<Tip>
  For virtual replicas, you only need to include the [`customRanking`](/doc/api-reference/api-parameters/customRanking) attribute used for sorting: `price`.
</Tip>

## Example: exhaustive sort by price

This example applies exhaustive sorting to a standard replica.

<CodeGroup>
  ```cs C# theme={"system"}
  var response = await client.SetSettingsAsync(
    "INDEX_NAME",
    new IndexSettings
    {
      Ranking = new List<string>
      {
        "desc(price)",
        "typo",
        "geo",
        "words",
        "filters",
        "proximity",
        "attribute",
        "exact",
        "custom",
      },
    }
  );
  ```

  ```dart Dart theme={"system"}
  final response = await client.setSettings(
    indexName: "INDEX_NAME",
    indexSettings: IndexSettings(
      ranking: [
        "desc(price)",
        "typo",
        "geo",
        "words",
        "filters",
        "proximity",
        "attribute",
        "exact",
        "custom",
      ],
    ),
  );
  ```

  ```go Go theme={"system"}
  response, err := client.SetSettings(client.NewApiSetSettingsRequest(
    "INDEX_NAME",
    search.NewEmptyIndexSettings().SetRanking(
      []string{"desc(price)", "typo", "geo", "words", "filters", "proximity", "attribute", "exact", "custom"})))
  if err != nil {
    // handle the eventual error
    panic(err)
  }
  ```

  ```java Java theme={"system"}
  UpdatedAtResponse response = client.setSettings(
    "INDEX_NAME",
    new IndexSettings().setRanking(
      Arrays.asList("desc(price)", "typo", "geo", "words", "filters", "proximity", "attribute", "exact", "custom")
    )
  );
  ```

  ```js JavaScript theme={"system"}
  const response = await client.setSettings({
    indexName: 'theIndexName',
    indexSettings: {
      ranking: ['desc(price)', 'typo', 'geo', 'words', 'filters', 'proximity', 'attribute', 'exact', 'custom'],
    },
  });
  ```

  ```kotlin Kotlin theme={"system"}
  var response =
    client.setSettings(
      indexName = "INDEX_NAME",
      indexSettings =
        IndexSettings(
          ranking =
            listOf(
              "desc(price)",
              "typo",
              "geo",
              "words",
              "filters",
              "proximity",
              "attribute",
              "exact",
              "custom",
            )
        ),
    )
  ```

  ```php PHP theme={"system"}
  $response = $client->setSettings(
      'INDEX_NAME',
      ['ranking' => [
          'desc(price)',

          'typo',

          'geo',

          'words',

          'filters',

          'proximity',

          'attribute',

          'exact',

          'custom',
      ],
      ],
  );
  ```

  ```python Python theme={"system"}
  response = client.set_settings(
      index_name="INDEX_NAME",
      index_settings={
          "ranking": [
              "desc(price)",
              "typo",
              "geo",
              "words",
              "filters",
              "proximity",
              "attribute",
              "exact",
              "custom",
          ],
      },
  )
  ```

  ```ruby Ruby theme={"system"}
  response = client.set_settings(
    "INDEX_NAME",
    Algolia::Search::IndexSettings.new(
      ranking: ["desc(price)", "typo", "geo", "words", "filters", "proximity", "attribute", "exact", "custom"]
    )
  )
  ```

  ```scala Scala theme={"system"}
  val response = Await.result(
    client.setSettings(
      indexName = "INDEX_NAME",
      indexSettings = IndexSettings(
        ranking =
          Some(Seq("desc(price)", "typo", "geo", "words", "filters", "proximity", "attribute", "exact", "custom"))
      )
    ),
    Duration(100, "sec")
  )
  ```

  ```swift Swift theme={"system"}
  let response = try await client.setSettings(
      indexName: "INDEX_NAME",
      indexSettings: IndexSettings(ranking: [
          "desc(price)",
          "typo",
          "geo",
          "words",
          "filters",
          "proximity",
          "attribute",
          "exact",
          "custom",
      ])
  )
  ```
</CodeGroup>

<Check>
  For standard replicas, you need to provide **all** ranking attributes.
</Check>
