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

# customRanking

> Attributes to use for custom ranking

export const Setting = ({type, default: defaultValue, defaultNote, scope, min, max, formerly}) => {
  const renderedDefault = defaultValue === '' ? '""' : defaultValue;
  const renderedNote = defaultNote ? `(${defaultNote})` : '';
  return <ul>
      <li><strong>Type:</strong> <code>{type}</code></li>
      <li><strong>Default:</strong> <code>{renderedDefault}</code>{renderedNote}</li>
      {min && <li><strong>Min:</strong> <code>{min}</code></li>}
      {max && <li><strong>Max:</strong> <code>{max}</code></li>}
      <li><strong>Scope:</strong> <a href="/doc/api-reference/api-parameters"><code>{scope}</code></a></li>
      {formerly && <li>
          <strong>Deprecated name:</strong> <code>{formerly}</code>
        </li>}
    </ul>;
};

<Setting type="list<string>" default="[]" defaultNote="no custom ranking" scope="settings" />

Algolia's [tie-breaking algorithm](/doc/guides/managing-results/relevance-overview/in-depth/ranking-criteria)
applies the custom ranking attributes in the order they're listed.
Use `asc(attribute)` or `desc(attribute)` to specify the sort direction for each attribute.

To learn more, see [Custom ranking](/doc/guides/managing-results/must-do/custom-ranking).

## Usage

* Attribute names are case-sensitive.
* Records with missing or `null` values for a custom ranking attribute are considered equal and are placed at the end of the result list, regardless of sort order.
* Boolean values are sorted alphabetically: `false` comes before `true` in ascending order, and `true` comes before `false` in descending order.

## Modifiers

<ParamField path="asc">
  Sort by increasing value of the attribute.
</ParamField>

<ParamField path="desc">
  Sort by decreasing value of the attribute.
</ParamField>

## Example

The following example ranks tied records by decreasing popularity.
Records with equal popularity will be ranked by ascending price.

<AccordionGroup>
  <Accordion title="Current API clients" defaultOpen="true">
    <CodeGroup>
      ```cs C# theme={"system"}
      var response = await client.SetSettingsAsync(
        "INDEX_NAME",
        new IndexSettings
        {
          CustomRanking = new List<string> { "desc(popularity)", "asc(price)" },
        }
      );
      ```

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

      ```go Go theme={"system"}
      response, err := client.SetSettings(client.NewApiSetSettingsRequest(
        "INDEX_NAME",
        search.NewEmptyIndexSettings().SetCustomRanking(
          []string{"desc(popularity)", "asc(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(popularity)", "asc(price)"))
      );
      ```

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

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

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

              'asc(price)',
          ],
          ],
      );
      ```

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

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

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

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

  <Accordion title="Legacy API clients">
    <CodeGroup>
      ```cs C# theme={"system"}
      IndexSettings settings = new IndexSettings();

      settings.CustomRanking = new List
      {
          "desc(popularity)",
          "asc(price)"
      };

      index.SetSettings(settings);
      ```

      ```go Go theme={"system"}
      res, err := index.SetSettings(search.Settings{
      	CustomRanking: opt.CustomRanking(
      		"desc(popularity)",
      		"asc(price)",
      	),
      })
      ```

      ```java Java theme={"system"}
      index.setSettings(
        new IndexSettings().setCustomRanking(Arrays.asList(
          "desc(popularity)",
          "asc(price)"
        ))
      );
      ```

      ```js JavaScript theme={"system"}
      index
        .setSettings({
          customRanking: ["desc(popularity)", "asc(price)"],
        })
        .then(() => {
          // done
        });
      ```

      ```kotlin Kotlin theme={"system"}
      val settings = settings {
          customRanking {
              +Desc("popularity")
              +Asc("price")
          }
      }

      index.setSettings(settings)
      ```

      ```php PHP theme={"system"}
      $index->setSettings([
        'customRanking' => [
          'desc(popularity)',
          'asc(price)'
        ]
      ]);
      ```

      ```python Python theme={"system"}
      index.set_settings({"customRanking": ["desc(popularity)", "asc(price)"]})
      ```

      ```ruby Ruby theme={"system"}
      index.set_settings(
        {
          customRanking: [
            "desc(popularity)",
            "asc(price)"
          ]
        }
      )
      ```

      ```scala Scala theme={"system"}
      client.execute {
        setSettings of "myIndex" `with` IndexSettings(
          customRanking = Some(Seq(
            CustomRanking.desc("popularity"),
            CustomRanking.asc("price")
          ))
        )
      }
      ```

      ```swift Swift theme={"system"}
      let settings = Settings()
        .set(\.customRanking, to: [
          .desc("popularity"),
          .asc("price")
        ])

      index.setSettings(settings) { result in
        if case .success(let response) = result {
          print("Response: \(response)")
        }
      }
      ```
    </CodeGroup>
  </Accordion>
</AccordionGroup>
