Sorting
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:
Request
Response
Field Description
https://soccer.sportmonks.com/api/v2.0/seasons/17141?api_token={API_TOKEN}&include=fixtures
{
"data": {
"id": 17141,
"name": "2020/2021",
"league_id": 501,
"is_current_season": true,
"current_round_id": 194980,
"current_stage_id": 77447501,
"fixtures": {
"data": [
{
"id": 16475283,
"league_id": 501,
"season_id": 17141,
"stage_id": 77447501,
"round_id": 194968,
"group_id": null,
"aggregate_id": null,
"venue_id": 281425,
"referee_id": 15818,
"localteam_id": 496,
"visitorteam_id": 258,
"winner_team_id": 496,
"weather_report": {
"code": "clouds",
"type": "broken clouds",
"icon": "https://cdn.sportmonks.com/images/weather/04d.png",
"temperature": {
"temp": 60.62,
"unit": "fahrenheit"
},
"temperature_celcius": {
"temp": 15.9,
"unit": "celcius"
},
"clouds": "75%",
"humidity": "72%",
"pressure": 1013,
"wind": {
"speed": "5.82 m/s",
"degree": 300
},
"coordinates": {
"lat": 55.83,
"lon": -4.43
},
"updated_at": "2020-08-01T15:45:06.417645Z"
},
"commentaries": false,
"attendance": null,
"pitch": null,
"details": null,
"neutral_venue": false,
"winning_odds_calculated": true,
"formations": {
"localteam_formation": "4-2-3-1",
"visitorteam_formation": "3-4-2-1"
},
"scores": {
"localteam_score": 1,
"visitorteam_score": 0,
"localteam_pen_score": null,
"visitorteam_pen_score": null,
"ht_score": "1-0",
"ft_score": "1-0",
"et_score": null,
"ps_score": null
},
"time": {
"status": "FT",
"starting_at": {
"date_time": "2020-08-01 14:00:00",
"date": "2020-08-01",
"time": "14:00:00",
"timestamp": 1596290400,
"timezone": "UTC"
},
"minute": 90,
"second": null,
"added_time": null,
"extra_minute": null,
"injury_time": null
},
"coaches": {
"localteam_coach_id": 1621,
"visitorteam_coach_id": 22145896
},
"standings": {
"localteam_position": 3,
"visitorteam_position": 12
},
"assistants": {
"first_assistant_id": 12770,
"second_assistant_id": 36793,
"fourth_official_id": 19329
},
"leg": "1/1",
"colors": {
"localteam": {
"color": "#F0F0F0",
"kit_colors": "#F0F0F0,#F0F0F0,#0A0A0A,#0A0A0A,#0A0A0A,#FFDF1B,#F0F0F0"
},
"visitorteam": {
"color": "#FDBD0F",
"kit_colors": "#FDBD0F,#FDBD0F,#FAE403,#FAE403,#5C8FAE,#FFDF1B,#FDBD0F"
}
},
"deleted": false
"is_placeholder": false
},
{
"id": 16475284,
"league_id": 501,
"season_id": 17141,
"stage_id": 77447501,
"round_id": 194968,
"group_id": null,
"aggregate_id": null,
"venue_id": 8947,
"referee_id": 18748,
"localteam_id": 282,
"visitorteam_id": 734,
"winner_team_id": null,
"weather_report": {
"code": "clouds",
"type": "scattered clouds",
"icon": "https://cdn.sportmonks.com/images/weather/03d.png",
"temperature": {
"temp": 64.83,
"unit": "fahrenheit"
},
"temperature_celcius": {
"temp": 18.2,
"unit": "celcius"
},
"clouds": "40%",
"humidity": "68%",
"pressure": 1011,
"wind": {
"speed": "16.11 m/s",
"degree": 240
},
"coordinates": {
"lat": 56.5,
"lon": -2.97
},
"updated_at": "2020-08-01T15:45:06.463564Z"
},
"commentaries": false,
"attendance": null,
"pitch": null,
"details": null,
"neutral_venue": false,
"winning_odds_calculated": true,
"formations": {
"localteam_formation": "3-5-2",
"visitorteam_formation": "3-4-3"
},
"scores": {
"localteam_score": 1,
"visitorteam_score": 1,
"localteam_pen_score": null,
"visitorteam_pen_score": null,
"ht_score": "1-0",
"ft_score": "1-1",
"et_score": null,
"ps_score": null
},
"time": {
"status": "FT",
"starting_at": {
"date_time": "2020-08-01 14:00:00",
"date": "2020-08-01",
"time": "14:00:00",
"timestamp": 1596290400,
"timezone": "UTC"
},
"minute": 90,
"second": null,
"added_time": null,
"extra_minute": null,
"injury_time": null
},
"coaches": {
"localteam_coach_id": 896498,
"visitorteam_coach_id": 1742
},
"standings": {
"localteam_position": 4,
"visitorteam_position": 5
},
"assistants": {
"first_assistant_id": 14467,
"second_assistant_id": 12123,
"fourth_official_id": 17256
},
"leg": "1/1",
"colors": {
"localteam": {
"color": "#FC7E00",
"kit_colors": "#FC7E00,#FC7E00,#FC7E00,#FC7E00,#FB6614,#FB6614,#0A0A0A"
},
"visitorteam": {
"color": "#0046A8",
"kit_colors": "#0046A8,#0046A8,#2B72DE,#2B72DE,#1B6FC5,#1B6FC5,#F0F0F0"
}
},
"deleted": false
"deleted": false
},
Season Field Description
Field
Description
data
Opens an array of data you've requested
id
The unique season id
name
The name of the season
league_id
The unique league id the season belongs to
is_current_season
Indicates if the season is the current one. Possible values are "true" or "false"
current_round_id
The unique id of the current round
current_stage_id
The unique id of the current stage
Fixtures Field Description
Field
Description
data
Opens an array of data you've requested
id
A unique fixture id
league_id
The unique id of the league the fixture belongs to
season_id
The unique id of the season the fixture belongs to
stage_id
The unique id of the stage the fixture belongs to
round_id
The unique id of the round the fixture belongs to
group_id
The unique id of the group the fixture belongs to
aggregate_id
Indicates if the fixture has an aggregated score
venue_id
The unique id of the venue the fixture is played at
referee_id
The unique id of the referee that is in charge of the fixture
localteam_id
The unique id of the local team
visitorteam_id
The unique id of the visitor team
winnerteam_id
Contains the id of the team that won the fixture
weather_report
Opens an array of weather details. Possible values of the data in this array can be found in our statuses and definitions
commentaries
Indicates if this fixture has commentaries available
attendance
Give information about the visitor attendance of the fixture
pitch
Gives information about the pitch. Possible values can be found in our statuses and definitions
details
Optional additional information.
neutral_venue
Indicates if the fixture was played at a neutral fixture. This often happens in cup finals
formations
Opens an array containing the lineup formations of the teams. Possible values can be found in our statuses and definitions
scores
Opens an array containing the scores of the fixture
localteam_score
The amount of goals the local team has scored in this fixture
visitorteam_score
The amount of goals the visitor team has scored in this fixture
localteam_pen_score
The amount of penalty goals the local team has scored in this fixture (only available when the fixture goes into penalty shootout)
visitorteam_pen_score
The amount of penalty goals the visitor team has scored in this fixture (only available when the fixture goes into penalty shootout)
ht_score
The score at half time
ft_score
The score at full time (90 minutes)
et_score
The score in extra time (only available when the fixture goes into extra time)
ps_score
The score of the penalty shootout (only available when the fixture goes into penalty shootout)
time
Opens an array containing the time details of the fixture
status
Indicates in which phase the fixture is in. Possible values can be found in our statuses and definitions
starting_at
Opens an array containing the time details of the fixture
date_time
Gives the date and time the fixture starts
date
Gives the date on which the fixture is played
time
Gives the starting time of the fixture
timestamp
The timestamp notation of the match
timezone
Indicates in which timezone you've requested the data
minute
Indicates in which minute the fixture is in
second
Indicates in which second the fixture is in
added_time
Indicates how many added time is added
extra_minute
Extra minute is when a fixture goes to extra time. Extra_minute will count from 0 to 15 and from 15 to 30.
injury_time
In case of added time it will end up in injury_minute
coaches
Opens an array containing the unique id's of the coaches
localteam_coach_id
The unique id of the coach from the local team
visitorteam_coach_id
The unique id of the coach from the visitor team
standings
Opens an array containing the standings of the local- and visitorteam
localteam_position
Indicates in which positions the local team is before the fixture
visitorteam_position
Indicates in which positions the visitor team is before the fixture
assistants
Opens an array containing the unique id's of the assistant referees
first_assistant_id
The unique id of the first assistant
second_assistant_id
The unique id of the second assistant
fourth_official_id
The unique id of the fourth official
leg
Indicates in how many legs this fixture is played
colors
Opens an array containing information about the team colors
localteam
Opens an array containing information about the local team colors
color
The main color code of the local team
kit_colors
The kit color codes of the local team
visitorteam
Opens an array containing information about the visitor team colors
color
The main color code of the visitor team
kit_colors
The kit color codes of the local team
deleted
Indicates if the team was deleted. More information: Keep in Sync
is_placeholder
This property indicates if the resource is used to display dummy data. The false of this property will always be a boolean value.
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:
Request
Response
https://soccer.sportmonks.com/api/v2.0/seasons/17141?api_token={API_TOKEN}&include=fixtures:order(starting_at|asc)
{
"data": {
"id": 17141,
"name": "2020/2021",
"league_id": 501,
"is_current_season": true,
"current_round_id": 194980,
"current_stage_id": 77447501,
"fixtures": {
"data": [
{
"id": 16475286,
"league_id": 501,
"season_id": 17141,
"stage_id": 77447501,
"round_id": 194968,
"group_id": null,
"aggregate_id": null,
"venue_id": 8928,
"referee_id": 14853,
"localteam_id": 273,
"visitorteam_id": 62,
"winner_team_id": 62,
"weather_report": {
"code": "clouds",
"type": "overcast clouds",
"icon": "https://cdn.sportmonks.com/images/weather/04d.png",
"temperature": {
"temp": 61.7,
"unit": "fahrenheit"
},
"temperature_celcius": {
"temp": 16.5,
"unit": "celcius"
},
"clouds": "90%",
"humidity": "87%",
"pressure": 1011,
"wind": {
"speed": "5.82 m/s",
"degree": 120
},
"coordinates": {
"lat": 57.14,
"lon": -2.1
},
"updated_at": "2020-08-01T13:15:06.433081Z"
},
"commentaries": false,
"attendance": null,
"pitch": null,
"details": null,
"neutral_venue": false,
"winning_odds_calculated": true,
"formations": {
"localteam_formation": "4-4-1-1",
"visitorteam_formation": "4-3-3"
},
"scores": {
"localteam_score": 0,
"visitorteam_score": 1,
"localteam_pen_score": null,
"visitorteam_pen_score": null,
"ht_score": "0-1",
"ft_score": "0-1",
"et_score": null,
"ps_score": null
},
"time": {
"status": "FT",
"starting_at": {
"date_time": "2020-08-01 11:30:00",
"date": "2020-08-01",
"time": "11:30:00",
"timestamp": 1596281400,
"timezone": "UTC"
},
"minute": 90,
"second": null,
"added_time": null,
"extra_minute": null,
"injury_time": null
},
"coaches": {
"localteam_coach_id": 896462,
"visitorteam_coach_id": 50
},
"standings": {
"localteam_position": 11,
"visitorteam_position": 2
},
"assistants": {
"first_assistant_id": 15817,
"second_assistant_id": 12772,
"fourth_official_id": 17847
},
"leg": "1/1",
"colors": {
"localteam": {
"color": "#C40010",
"kit_colors": "#C40010,#C40010,#CA1313,#CA1313,#FF1B1B,#FF1B1B,#C40010"
},
"visitorteam": {
"color": "#F0F0F0",
"kit_colors": "#F0F0F0,#F0F0F0,#262626,#F0F0F0,#C40010,#0046A8,#002B87"
}
},
"deleted": false
"is_placeholder": false
},
{
"id": 16475283,
"league_id": 501,
"season_id": 17141,
"stage_id": 77447501,
"round_id": 194968,
"group_id": null,
"aggregate_id": null,
"venue_id": 281425,
"referee_id": 15818,
"localteam_id": 496,
"visitorteam_id": 258,
"winner_team_id": 496,
"weather_report": {
"code": "clouds",
"type": "broken clouds",
"icon": "https://cdn.sportmonks.com/images/weather/04d.png",
"temperature": {
"temp": 60.62,
"unit": "fahrenheit"
},
"temperature_celcius": {
"temp": 15.9,
"unit": "celcius"
},
"clouds": "75%",
"humidity": "72%",
"pressure": 1013,
"wind": {
"speed": "5.82 m/s",
"degree": 300
},
"coordinates": {
"lat": 55.83,
"lon": -4.43
},
"updated_at": "2020-08-01T15:45:06.417645Z"
},
"commentaries": false,
"attendance": null,
"pitch": null,
"details": null,
"neutral_venue": false,
"winning_odds_calculated": true,
"formations": {
"localteam_formation": "4-2-3-1",
"visitorteam_formation": "3-4-2-1"
},
"scores": {
"localteam_score": 1,
"visitorteam_score": 0,
"localteam_pen_score": null,
"visitorteam_pen_score": null,
"ht_score": "1-0",
"ft_score": "1-0",
"et_score": null,
"ps_score": null
},
"time": {
"status": "FT",
"starting_at": {
"date_time": "2020-08-01 14:00:00",
"date": "2020-08-01",
"time": "14:00:00",
"timestamp": 1596290400,
"timezone": "UTC"
},
"minute": 90,
"second": null,
"added_time": null,
"extra_minute": null,
"injury_time": null
},
"coaches": {
"localteam_coach_id": 1621,
"visitorteam_coach_id": 22145896
},
"standings": {
"localteam_position": 3,
"visitorteam_position": 12
},
"assistants": {
"first_assistant_id": 12770,
"second_assistant_id": 36793,
"fourth_official_id": 19329
},
"leg": "1/1",
"colors": {
"localteam": {
"color": "#F0F0F0",
"kit_colors": "#F0F0F0,#F0F0F0,#0A0A0A,#0A0A0A,#0A0A0A,#FFDF1B,#F0F0F0"
},
"visitorteam": {
"color": "#FDBD0F",
"kit_colors": "#FDBD0F,#FDBD0F,#FAE403,#FAE403,#5C8FAE,#FFDF1B,#FDBD0F"
}
},
"deleted": false
"is_placeholder": false
},
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?
Request
Response
https://soccer.sportmonks.com/api/v2.0/seasons/17141?api_token={API_TOKEN}&include=fixtures:order(starting_at|desc)
{
"data": {
"id": 17141,
"name": "2020/2021",
"league_id": 501,
"is_current_season": true,
"current_round_id": 194980,
"current_stage_id": 77447501,
"fixtures": {
"data": [
{
"id": 16475480,
"league_id": 501,
"season_id": 17141,
"stage_id": 77447501,
"round_id": 195000,
"group_id": null,
"aggregate_id": null,
"venue_id": null,
"referee_id": null,
"localteam_id": 496,
"visitorteam_id": 258,
"winner_team_id": null,
"weather_report": null,
"commentaries": false,
"attendance": null,
"pitch": null,
"details": null,
"neutral_venue": false,
"winning_odds_calculated": false,
"formations": {
"localteam_formation": null,
"visitorteam_formation": null
},
"scores": {
"localteam_score": 0,
"visitorteam_score": 0,
"localteam_pen_score": null,
"visitorteam_pen_score": null,
"ht_score": null,
"ft_score": null,
"et_score": null,
"ps_score": null
},
"time": {
"status": "NS",
"starting_at": {
"date_time": "2021-04-03 14:00:00",
"date": "2021-04-03",
"time": "14:00:00",
"timestamp": 1617458400,
"timezone": "UTC"
},
"minute": null,
"second": null,
"added_time": null,
"extra_minute": null,
"injury_time": null
},
"coaches": {
"localteam_coach_id": null,
"visitorteam_coach_id": null
},
"standings": {
"localteam_position": null,
"visitorteam_position": null
},
"assistants": {
"first_assistant_id": null,
"second_assistant_id": null,
"fourth_official_id": null
},
"leg": "1/1",
"colors": null,
"deleted": false
"is_placeholder": false
},
{
"id": 16475479,
"league_id": 501,
"season_id": 17141,
"stage_id": 77447501,
"round_id": 195000,
"group_id": null,
"aggregate_id": null,
"venue_id": null,
"referee_id": null,
"localteam_id": 246,
"visitorteam_id": 53,
"winner_team_id": null,
"weather_report": null,
"commentaries": false,
"attendance": null,
"pitch": null,
"details": null,
"neutral_venue": false,
"winning_odds_calculated": false,
"formations": {
"localteam_formation": null,
"visitorteam_formation": null
},
"scores": {
"localteam_score": 0,
"visitorteam_score": 0,
"localteam_pen_score": null,
"visitorteam_pen_score": null,
"ht_score": null,
"ft_score": null,
"et_score": null,
"ps_score": null
},
"time": {
"status": "NS",
"starting_at": {
"date_time": "2021-04-03 14:00:00",
"date": "2021-04-03",
"time": "14:00:00",
"timestamp": 1617458400,
"timezone": "UTC"
},
"minute": null,
"second": null,
"added_time": null,
"extra_minute": null,
"injury_time": null
},
"coaches": {
"localteam_coach_id": null,
"visitorteam_coach_id": null
},
"standings": {
"localteam_position": null,
"visitorteam_position": null
},
"assistants": {
"first_assistant_id": null,
"second_assistant_id": null,
"fourth_official_id": null
},
"leg": "1/1",
"colors": null,
"deleted": false
"is_placeholder": false
},
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.
Export as PDF
Copy link
Outline
What is sorting?
Create the request