Guides / Managing results / Refine results / Sorting results

Sort an Index Alphabetically

Having constantly seen data sorted alphabetically, you may find yourself wanting to follow suit. However, while sorting alphabetically makes sense for a database, it doesn’t for a search engine and we strongly discourage it. This isn’t to say your use case is invalid; rather, we want to encourage caution and due consideration.

When you sort your data alphabetically, you don’t only devalue Algolia’s tie-breaking algorithm: you essentially disable it. If you sort alphabetically first, then the only objects requiring tie-breaking are those with the exact same name: there will likely be few objects that fit this criterea.

Alphabetically structured results lead users to search through data by hand, while a search engine, like Algolia, should ensure that they don’t need to.

Warnings aside, it’s still possible to sort alphabetically by using the customRanking parameter with the attribute you want to sort on.

Using the API

To sort an index alphabetically, you need to modify its custom ranking, and set custom as the index’s first ranking criterion.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$index->setSettings([
   'customRanking' => [
     'asc(textual_attribute)'
   ],
   'ranking' => [
      'custom',
      'typo',
      'geo',
      'words',
      'filters',
      'proximity',
      'attribute',
      'exact'
    ]
]);

Caveats

Algolia is not locale-aware. Strings are ordered by lexicographical order of their Unicode characters. That may be acceptable for English text, less so for other languages (especially those with diacritics).

Did you find this page helpful?