After filtering and limiting, comes sorting! In this tutorial, we’ll teach you how to sort the API response and why this might come in handy for you. In other words, you will learn how to order the API response on a specific field.

What is sorting?

Sorting as its name implies, is the act of having the API sort data in an ascending or descending logical order. So, we have the following numbers: 8, 3, 7, 5, 1 If we sort this in ascending form: 1, 3, 5, 7, 8 Descending form: 8, 7, 5, 3, 1

Now, let's apply this logic to our API. Every fixture is assigned a unique id at the announcement of a new season. Our API logically orders these ids. The first fixture’s id ends with one, the second fixture’s id with two, etc. However, during the season, games get rescheduled all the time. This will most of the time result in updated games, but since the game ids haven't changed, it might result in weird responses.

Let’s say fixture #5 is scheduled to be played in round one but gets rescheduled and will now be played in the last round of the season. The API still lists the fixture as the fifth match, but the start date is now much later in the season. Therefore, the response is not in order of the original starting date.

To avoid this, we want the API to sort the response with all the fixtures in the correctly scheduled order. We can do this by using the attribute called starting_at.

Note that you can only sort on included data. When you’ve included the fixtures, you can sort on all the fields in the fixture data.

Create the request

First, the standard request without the sorting:{API_TOKEN}&include=fixtures

This URL requests all the fixtures of the 20/21 season of the Scottish Premiership (season id: 17141). Second, we add in the :order(starting_at | asc) to sort the request in the ascending order:{API_TOKEN}&include=fixtures:order(starting_at|asc)

The above request will give you all the matches sorted by the starting_at attribute in ascending (asc) order, but you can also request it in the descending (desc) order.

This sorting option will get you all the fixtures in the preferred scheduled order. See how the order changes based on the starting_at attribute?{API_TOKEN}&include=fixtures:order(starting_at|desc)

And that’s all there is to sorting API requests!

With that, we have reached the end of our Sorting tutorial. In the next chapter, we’ll discuss one of the most important endpoints, namely season schedule, fixtures and livescores.

Last updated