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
1
https://soccer.sportmonks.com/api/v2.0/seasons/17141?api_token={API_TOKEN}&include=fixtures
Copied!
1
{
2
"data": {
3
"id": 17141,
4
"name": "2020/2021",
5
"league_id": 501,
6
"is_current_season": true,
7
"current_round_id": 194980,
8
"current_stage_id": 77447501,
9
"fixtures": {
10
"data": [
11
{
12
"id": 16475283,
13
"league_id": 501,
14
"season_id": 17141,
15
"stage_id": 77447501,
16
"round_id": 194968,
17
"group_id": null,
18
"aggregate_id": null,
19
"venue_id": 281425,
20
"referee_id": 15818,
21
"localteam_id": 496,
22
"visitorteam_id": 258,
23
"winner_team_id": 496,
24
"weather_report": {
25
"code": "clouds",
26
"type": "broken clouds",
27
"icon": "https://cdn.sportmonks.com/images/weather/04d.png",
28
"temperature": {
29
"temp": 60.62,
30
"unit": "fahrenheit"
31
},
32
"temperature_celcius": {
33
"temp": 15.9,
34
"unit": "celcius"
35
},
36
"clouds": "75%",
37
"humidity": "72%",
38
"pressure": 1013,
39
"wind": {
40
"speed": "5.82 m/s",
41
"degree": 300
42
},
43
"coordinates": {
44
"lat": 55.83,
45
"lon": -4.43
46
},
47
"updated_at": "2020-08-01T15:45:06.417645Z"
48
},
49
"commentaries": false,
50
"attendance": null,
51
"pitch": null,
52
"details": null,
53
"neutral_venue": false,
54
"winning_odds_calculated": true,
55
"formations": {
56
"localteam_formation": "4-2-3-1",
57
"visitorteam_formation": "3-4-2-1"
58
},
59
"scores": {
60
"localteam_score": 1,
61
"visitorteam_score": 0,
62
"localteam_pen_score": null,
63
"visitorteam_pen_score": null,
64
"ht_score": "1-0",
65
"ft_score": "1-0",
66
"et_score": null,
67
"ps_score": null
68
},
69
"time": {
70
"status": "FT",
71
"starting_at": {
72
"date_time": "2020-08-01 14:00:00",
73
"date": "2020-08-01",
74
"time": "14:00:00",
75
"timestamp": 1596290400,
76
"timezone": "UTC"
77
},
78
"minute": 90,
79
"second": null,
80
"added_time": null,
81
"extra_minute": null,
82
"injury_time": null
83
},
84
"coaches": {
85
"localteam_coach_id": 1621,
86
"visitorteam_coach_id": 22145896
87
},
88
"standings": {
89
"localteam_position": 3,
90
"visitorteam_position": 12
91
},
92
"assistants": {
93
"first_assistant_id": 12770,
94
"second_assistant_id": 36793,
95
"fourth_official_id": 19329
96
},
97
"leg": "1/1",
98
"colors": {
99
"localteam": {
100
"color": "#F0F0F0",
101
"kit_colors": "#F0F0F0,#F0F0F0,#0A0A0A,#0A0A0A,#0A0A0A,#FFDF1B,#F0F0F0"
102
},
103
"visitorteam": {
104
"color": "#FDBD0F",
105
"kit_colors": "#FDBD0F,#FDBD0F,#FAE403,#FAE403,#5C8FAE,#FFDF1B,#FDBD0F"
106
}
107
},
108
"deleted": false
109
"is_placeholder": false
110
},
111
{
112
"id": 16475284,
113
"league_id": 501,
114
"season_id": 17141,
115
"stage_id": 77447501,
116
"round_id": 194968,
117
"group_id": null,
118
"aggregate_id": null,
119
"venue_id": 8947,
120
"referee_id": 18748,
121
"localteam_id": 282,
122
"visitorteam_id": 734,
123
"winner_team_id": null,
124
"weather_report": {
125
"code": "clouds",
126
"type": "scattered clouds",
127
"icon": "https://cdn.sportmonks.com/images/weather/03d.png",
128
"temperature": {
129
"temp": 64.83,
130
"unit": "fahrenheit"
131
},
132
"temperature_celcius": {
133
"temp": 18.2,
134
"unit": "celcius"
135
},
136
"clouds": "40%",
137
"humidity": "68%",
138
"pressure": 1011,
139
"wind": {
140
"speed": "16.11 m/s",
141
"degree": 240
142
},
143
"coordinates": {
144
"lat": 56.5,
145
"lon": -2.97
146
},
147
"updated_at": "2020-08-01T15:45:06.463564Z"
148
},
149
"commentaries": false,
150
"attendance": null,
151
"pitch": null,
152
"details": null,
153
"neutral_venue": false,
154
"winning_odds_calculated": true,
155
"formations": {
156
"localteam_formation": "3-5-2",
157
"visitorteam_formation": "3-4-3"
158
},
159
"scores": {
160
"localteam_score": 1,
161
"visitorteam_score": 1,
162
"localteam_pen_score": null,
163
"visitorteam_pen_score": null,
164
"ht_score": "1-0",
165
"ft_score": "1-1",
166
"et_score": null,
167
"ps_score": null
168
},
169
"time": {
170
"status": "FT",
171
"starting_at": {
172
"date_time": "2020-08-01 14:00:00",
173
"date": "2020-08-01",
174
"time": "14:00:00",
175
"timestamp": 1596290400,
176
"timezone": "UTC"
177
},
178
"minute": 90,
179
"second": null,
180
"added_time": null,
181
"extra_minute": null,
182
"injury_time": null
183
},
184
"coaches": {
185
"localteam_coach_id": 896498,
186
"visitorteam_coach_id": 1742
187
},
188
"standings": {
189
"localteam_position": 4,
190
"visitorteam_position": 5
191
},
192
"assistants": {
193
"first_assistant_id": 14467,
194
"second_assistant_id": 12123,
195
"fourth_official_id": 17256
196
},
197
"leg": "1/1",
198
"colors": {
199
"localteam": {
200
"color": "#FC7E00",
201
"kit_colors": "#FC7E00,#FC7E00,#FC7E00,#FC7E00,#FB6614,#FB6614,#0A0A0A"
202
},
203
"visitorteam": {
204
"color": "#0046A8",
205
"kit_colors": "#0046A8,#0046A8,#2B72DE,#2B72DE,#1B6FC5,#1B6FC5,#F0F0F0"
206
}
207
},
208
"deleted": false
209
"deleted": false
210
},
Copied!
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
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
1
https://soccer.sportmonks.com/api/v2.0/seasons/17141?api_token={API_TOKEN}&include=fixtures:order(starting_at|asc)
Copied!
1
{
2
"data": {
3
"id": 17141,
4
"name": "2020/2021",
5
"league_id": 501,
6
"is_current_season": true,
7
"current_round_id": 194980,
8
"current_stage_id": 77447501,
9
"fixtures": {
10
"data": [
11
{
12
"id": 16475286,
13
"league_id": 501,
14
"season_id": 17141,
15
"stage_id": 77447501,
16
"round_id": 194968,
17
"group_id": null,
18
"aggregate_id": null,
19
"venue_id": 8928,
20
"referee_id": 14853,
21
"localteam_id": 273,
22
"visitorteam_id": 62,
23
"winner_team_id": 62,
24
"weather_report": {
25
"code": "clouds",
26
"type": "overcast clouds",
27
"icon": "https://cdn.sportmonks.com/images/weather/04d.png",
28
"temperature": {
29
"temp": 61.7,
30
"unit": "fahrenheit"
31
},
32
"temperature_celcius": {
33
"temp": 16.5,
34
"unit": "celcius"
35
},
36
"clouds": "90%",
37
"humidity": "87%",
38
"pressure": 1011,
39
"wind": {
40
"speed": "5.82 m/s",
41
"degree": 120
42
},
43
"coordinates": {
44
"lat": 57.14,
45
"lon": -2.1
46
},
47
"updated_at": "2020-08-01T13:15:06.433081Z"
48
},
49
"commentaries": false,
50
"attendance": null,
51
"pitch": null,
52
"details": null,
53
"neutral_venue": false,
54
"winning_odds_calculated": true,
55
"formations": {
56
"localteam_formation": "4-4-1-1",
57
"visitorteam_formation": "4-3-3"
58
},
59
"scores": {
60
"localteam_score": 0,
61
"visitorteam_score": 1,
62
"localteam_pen_score": null,
63
"visitorteam_pen_score": null,
64
"ht_score": "0-1",
65
"ft_score": "0-1",
66
"et_score": null,
67
"ps_score": null
68
},
69
"time": {
70
"status": "FT",
71
"starting_at": {
72
"date_time": "2020-08-01 11:30:00",
73
"date": "2020-08-01",
74
"time": "11:30:00",
75
"timestamp": 1596281400,
76
"timezone": "UTC"
77
},
78
"minute": 90,
79
"second": null,
80
"added_time": null,
81
"extra_minute": null,
82
"injury_time": null
83
},
84
"coaches": {
85
"localteam_coach_id": 896462,
86
"visitorteam_coach_id": 50
87
},
88
"standings": {
89
"localteam_position": 11,
90
"visitorteam_position": 2
91
},
92
"assistants": {
93
"first_assistant_id": 15817,
94
"second_assistant_id": 12772,
95
"fourth_official_id": 17847
96
},
97
"leg": "1/1",
98
"colors": {
99
"localteam": {
100
"color": "#C40010",
101
"kit_colors": "#C40010,#C40010,#CA1313,#CA1313,#FF1B1B,#FF1B1B,#C40010"
102
},
103
"visitorteam": {
104
"color": "#F0F0F0",
105
"kit_colors": "#F0F0F0,#F0F0F0,#262626,#F0F0F0,#C40010,#0046A8,#002B87"
106
}
107
},
108
"deleted": false
109
"is_placeholder": false
110
},
111
{
112
"id": 16475283,
113
"league_id": 501,
114
"season_id": 17141,
115
"stage_id": 77447501,
116
"round_id": 194968,
117
"group_id": null,
118
"aggregate_id": null,
119
"venue_id": 281425,
120
"referee_id": 15818,
121
"localteam_id": 496,
122
"visitorteam_id": 258,
123
"winner_team_id": 496,
124
"weather_report": {
125
"code": "clouds",
126
"type": "broken clouds",
127
"icon": "https://cdn.sportmonks.com/images/weather/04d.png",
128
"temperature": {
129
"temp": 60.62,
130
"unit": "fahrenheit"
131
},
132
"temperature_celcius": {
133
"temp": 15.9,
134
"unit": "celcius"
135
},
136
"clouds": "75%",
137
"humidity": "72%",
138
"pressure": 1013,
139
"wind": {
140
"speed": "5.82 m/s",
141
"degree": 300
142
},
143
"coordinates": {
144
"lat": 55.83,
145
"lon": -4.43
146
},
147
"updated_at": "2020-08-01T15:45:06.417645Z"
148
},
149
"commentaries": false,
150
"attendance": null,
151
"pitch": null,
152
"details": null,
153
"neutral_venue": false,
154
"winning_odds_calculated": true,
155
"formations": {
156
"localteam_formation": "4-2-3-1",
157
"visitorteam_formation": "3-4-2-1"
158
},
159
"scores": {
160
"localteam_score": 1,
161
"visitorteam_score": 0,
162
"localteam_pen_score": null,
163
"visitorteam_pen_score": null,
164
"ht_score": "1-0",
165
"ft_score": "1-0",
166
"et_score": null,
167
"ps_score": null
168
},
169
"time": {
170
"status": "FT",
171
"starting_at": {
172
"date_time": "2020-08-01 14:00:00",
173
"date": "2020-08-01",
174
"time": "14:00:00",
175
"timestamp": 1596290400,
176
"timezone": "UTC"
177
},
178
"minute": 90,
179
"second": null,
180
"added_time": null,
181
"extra_minute": null,
182
"injury_time": null
183
},
184
"coaches": {
185
"localteam_coach_id": 1621,
186
"visitorteam_coach_id": 22145896
187
},
188
"standings": {
189
"localteam_position": 3,
190
"visitorteam_position": 12
191
},
192
"assistants": {
193
"first_assistant_id": 12770,
194
"second_assistant_id": 36793,
195
"fourth_official_id": 19329
196
},
197
"leg": "1/1",
198
"colors": {
199
"localteam": {
200
"color": "#F0F0F0",
201
"kit_colors": "#F0F0F0,#F0F0F0,#0A0A0A,#0A0A0A,#0A0A0A,#FFDF1B,#F0F0F0"
202
},
203
"visitorteam": {
204
"color": "#FDBD0F",
205
"kit_colors": "#FDBD0F,#FDBD0F,#FAE403,#FAE403,#5C8FAE,#FFDF1B,#FDBD0F"
206
}
207
},
208
"deleted": false
209
"is_placeholder": false
210
},
Copied!
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
1
https://soccer.sportmonks.com/api/v2.0/seasons/17141?api_token={API_TOKEN}&include=fixtures:order(starting_at|desc)
Copied!
1
{
2
"data": {
3
"id": 17141,
4
"name": "2020/2021",
5
"league_id": 501,
6
"is_current_season": true,
7
"current_round_id": 194980,
8
"current_stage_id": 77447501,
9
"fixtures": {
10
"data": [
11
{
12
"id": 16475480,
13
"league_id": 501,
14
"season_id": 17141,
15
"stage_id": 77447501,
16
"round_id": 195000,
17
"group_id": null,
18
"aggregate_id": null,
19
"venue_id": null,
20
"referee_id": null,
21
"localteam_id": 496,
22
"visitorteam_id": 258,
23
"winner_team_id": null,
24
"weather_report": null,
25
"commentaries": false,
26
"attendance": null,
27
"pitch": null,
28
"details": null,
29
"neutral_venue": false,
30
"winning_odds_calculated": false,
31
"formations": {
32
"localteam_formation": null,
33
"visitorteam_formation": null
34
},
35
"scores": {
36
"localteam_score": 0,
37
"visitorteam_score": 0,
38
"localteam_pen_score": null,
39
"visitorteam_pen_score": null,
40
"ht_score": null,
41
"ft_score": null,
42
"et_score": null,
43
"ps_score": null
44
},
45
"time": {
46
"status": "NS",
47
"starting_at": {
48
"date_time": "2021-04-03 14:00:00",
49
"date": "2021-04-03",
50
"time": "14:00:00",
51
"timestamp": 1617458400,
52
"timezone": "UTC"
53
},
54
"minute": null,
55
"second": null,
56
"added_time": null,
57
"extra_minute": null,
58
"injury_time": null
59
},
60
"coaches": {
61
"localteam_coach_id": null,
62
"visitorteam_coach_id": null
63
},
64
"standings": {
65
"localteam_position": null,
66
"visitorteam_position": null
67
},
68
"assistants": {
69
"first_assistant_id": null,
70
"second_assistant_id": null,
71
"fourth_official_id": null
72
},
73
"leg": "1/1",
74
"colors": null,
75
"deleted": false
76
"is_placeholder": false
77
},
78
{
79
"id": 16475479,
80
"league_id": 501,
81
"season_id": 17141,
82
"stage_id": 77447501,
83
"round_id": 195000,
84
"group_id": null,
85
"aggregate_id": null,
86
"venue_id": null,
87
"referee_id": null,
88
"localteam_id": 246,
89
"visitorteam_id": 53,
90
"winner_team_id": null,
91
"weather_report": null,
92
"commentaries": false,
93
"attendance": null,
94
"pitch": null,
95
"details": null,
96
"neutral_venue": false,
97
"winning_odds_calculated": false,
98
"formations": {
99
"localteam_formation": null,
100
"visitorteam_formation": null
101
},
102
"scores": {
103
"localteam_score": 0,
104
"visitorteam_score": 0,
105
"localteam_pen_score": null,
106
"visitorteam_pen_score": null,
107
"ht_score": null,
108
"ft_score": null,
109
"et_score": null,
110
"ps_score": null
111
},
112
"time": {
113
"status": "NS",
114
"starting_at": {
115
"date_time": "2021-04-03 14:00:00",
116
"date": "2021-04-03",
117
"time": "14:00:00",
118
"timestamp": 1617458400,
119
"timezone": "UTC"
120
},
121
"minute": null,
122
"second": null,
123
"added_time": null,
124
"extra_minute": null,
125
"injury_time": null
126
},
127
"coaches": {
128
"localteam_coach_id": null,
129
"visitorteam_coach_id": null
130
},
131
"standings": {
132
"localteam_position": null,
133
"visitorteam_position": null
134
},
135
"assistants": {
136
"first_assistant_id": null,
137
"second_assistant_id": null,
138
"fourth_official_id": null
139
},
140
"leg": "1/1",
141
"colors": null,
142
"deleted": false
143
"is_placeholder": false
144
},
Copied!
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