By design, Algolia provides one ranking strategy per index: when you want to provide different rankings for the same data, you need to use different indices for each ranking. These indices are called replicas.

To set up sorting by attribute, you first need to understand how replica indices work.

To sort by attribute, create a replica index and then change the ranking formula of the replica. You can do this from Algolia’s dashboard or through the API.

For chronological sorting, consider how Algolia handles dates. For example, you have a blog and want to create a replica to sort search results from the most recent to the oldest post. Because Algolia doesn’t interpret dates as ISO 8601 strings (such as “2008-09-15T15:53:00”), you must convert your dates into Unix timestamps (numeric values such as 1221486780) before sorting them.

Convert dates into Unix timestamps: an example

Before

Say you have an index called articles:

json
[
  {
    "post_title": "Let's start the adventure",
    "post_date": "2012-07-01",
    "author_name": "Nicolas Dessaigne",
    "author_image_url": "https://secure.gravatar.com/avatar/785489bc2ac2e08ae66648a8936c1101?s=40&d=mm&r=g",
    "permalink": "https://blog.algolia.com/lets-start-the-adventure/",
    "excerpt": "Welcome to The Algolia Blog! It's always difficult to write the first post of a blog! What should I talk about? The company, the founders, the business, the culture? And all that knowing that virtually nobody will read except diggers in a few years (hopefully)!\nLet's concentrate instead on what we'll be blogging about. Company news obviously, but not only. I expect we'll write quite a few posts about technology, algorithms, entrepreneurship, marketing, and whatever else we'll want to share with you 🙂\nAnd most important, feel free to participate in comments or by contacting us directly. We appreciate your feedback!\nWelcome to the Algolia blog!"
  },
  {
    "post_title": "Great discussions at LeWeb'12 London",
    "post_date": "2012-07-03",
    "author_name": "Nicolas Dessaigne",
    "author_image_url": "https://secure.gravatar.com/avatar/785489bc2ac2e08ae66648a8936c1101?s=40&d=mm&r=g",
    "permalink": "https://blog.algolia.com/great-discussions-at-leweb12-london/",
    "image": "https://blog.algolia.com/wp-content/uploads/2014/03/latency-360x200.png",
    "excerpt": "... take long for us to decide it was the way to go, since the perception of speed is so natural that the benefit far outweighs the longer integration code. We'll now work on simplifying it!\nWe'll soon do a post about this demo. In the meantime, stay tuned!\n \n "
  }
]

After

You want to create a replica that sorts your data by date. The problem is that the post_date attribute has dates formatted as strings, which Algolia can’t process for sorting. Before creating a replica, you must transform these dates into Unix timestamps.

You don’t have to remove or change post_date. Add a post_date_timestamp attribute with the proper format instead.

json
[
  {
    "post_title": "Let's start the adventure",
    "post_date": "2012-07-01",
    "post_date_timestamp": 1341100800,
    "author_name": "Nicolas Dessaigne",
    "author_image_url": "https://secure.gravatar.com/avatar/785489bc2ac2e08ae66648a8936c1101?s=40&d=mm&r=g",
    "permalink": "https://blog.algolia.com/lets-start-the-adventure/",
    "excerpt": "Welcome to The Algolia Blog! It's always difficult to write the first post of a blog! What should I talk about? The company, the founders, the business, the culture? And all that knowing that virtually nobody will read except diggers in a few years (hopefully)!\nLet's concentrate instead on what we'll be blogging about. Company news obviously, but not only. I expect we'll write quite a few posts about technology, algorithms, entrepreneurship, marketing, and whatever else we'll want to share with you 🙂\nAnd most important, feel free to participate in comments or by contacting us directly. We appreciate your feedback!\nWelcome to the Algolia blog!"
  },
  {
    "post_title": "Great discussions at LeWeb'12 London",
    "post_date": "2012-07-03",
    "post_date_timestamp": 1341273600,
    "author_name": "Nicolas Dessaigne",
    "author_image_url": "https://secure.gravatar.com/avatar/785489bc2ac2e08ae66648a8936c1101?s=40&d=mm&r=g",
    "permalink": "https://blog.algolia.com/great-discussions-at-leweb12-london/",
    "image": "https://blog.algolia.com/wp-content/uploads/2014/03/latency-360x200.png",
    "excerpt": "... take long for us to decide it was the way to go, since the perception of speed is so natural that the benefit far outweighs the longer integration code. We'll now work on simplifying it!\nWe'll soon do a post about this demo. In the meantime, stay tuned!\n \n "
  }
]

Create a replica

Now, create a replica of your articles index. The recommendation is to name your replica indices with a prefix or suffix describing its sorting strategy (for example, articles_date_desc).

First, create a standard or virtual replica (articles_date_desc) from the primary index. The primary index is likely sorted by relevance but you want to change that for articles_date_desc.

Then, use the post_date_timestamp attribute to sort the articles_date_desc virtual index by date in descending order:

Sort a standard replica

Sort a virtual replica