🎉 Try the public beta of the new docs site at algolia.com/doc-beta! 🎉
Guides / Managing results / Refine results / Sorting results

To sort an index by an attribute, you must first create a replica index, then update your index configuration.

You can choose between setting up:

Relevant sorting is available on the Build and Premium pricing plans.

To let users select between different rankings in your user interface, you also need to update your user interface—for example, by including the sort-by widget.

Algolia applies one ranking strategy to each index. If you want to rank results differently, like sorting by a specific attribute, you must create separate replica indices for each type of ranking.

Attribute format

You can only sort by attributes with boolean or numerical values. Check, that you index numerical attributes as numbers, not strings. If you want to sort by dates, make sure to represent them as numbers as well.

Configure an attribute for sorting in the Algolia dashboard

  1. Create a replica index.

  2. Refresh the dashboard page in your browser.

  3. Select the replica index.

    Select your replica index

  4. On the Configuration tab, go to Relevant sort or Ranking & Sorting. You’ll see either option, depending on the type of replica you want to configure (virtual or standard).

  5. To add an attribute for sorting, click +Add a sort attribute or +Add sort-by attribute and determine the sort direction (ascending or descending).

  6. Review and save your changes.

Configure an attribute for sorting with the API

  1. Create a replica index.
  2. Initialize the replica index.
  3. Update the ranking parameter of the replica index with the setSettings method.

Example: Relevant sort with a virtual replica

This example configures a Relevant sort with a virtual replica. The virtual replica reuses the ranking attribute from its primary index, so you only need to include the customRanking attribute that is used for sorting.

1
2
3
4
5
6
7
$replica_index = $client->initIndex('products_virtual_price_desc');

$replica_index->setSettings([
  'customRanking' => [
    'desc(price)'
  ]
]);

Example: Exhaustive sort with a standard replica

This example configures the ranking of a standard replica index to be sorted by price in descending order. You need to provide all ranking attributes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$replica_index = $client->initIndex('products_standard_price_desc');

$replica_index->setSettings([
  'ranking' => [
    'desc(price)',
    'typo',
    'geo',
    'words',
    'filters',
    'proximity',
    'attribute',
    'exact',
    'custom'
  ]
]);
Did you find this page helpful?