Sorting results
How to request a specific sort for the results of a collection of resources
When you fetch a collection of resources, you can request a specific sort for the results, using the sort query parameter.
The value of the sort parameter must be a comma-separated list of fields. Pay attention to avoid whitespaces before or after each comma.
The sort order for each field is ascending unless prefixed with a - (minus) in which case it's descending.

Examples

Sorting the parent resource

Request
Response
The following request fetches a collection of SKUs sorted alphabetically by their code:
1
curl -g -X GET \
2
'https://yourdomain.commercelayer.io/api/skus?sort=code' \
3
-H 'Accept: application/vnd.api+json' \
4
-H 'Authorization: Bearer your-access-token'
Copied!
On success, the API responds with a 200 OK status code, returning a paginated collection of the resource objects, sorted in the requested order:
1
{
2
"data": [
3
{
4
"id": "yzXKjYzaCx",
5
"type": "skus",
6
"links": {...},
7
"attributes": {
8
"code": "BABYONBU000000E63E7412MX",
9
"name": "Black Baby Onesie Short Sleeve with Pink Logo (12 Months)",
10
"description": "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
11
"image_url": "https://img.yourdomain.com/skus/yzXKjYzaCx.png",
12
"pieces_per_pack": 6,
13
"weight": 300.0,
14
"unit_of_weight": "gr",
15
"hs_tariff_number": null,
16
"do_not_ship": false,
17
"do_not_track": false,
18
"created_at": "2018-01-01T12:00:00.000Z",
19
"updated_at": "2018-01-01T12:00:00.000Z",
20
"reference": "ANYREFEFERNCE",
21
"metadata": {
22
"foo": "bar"
23
}
24
},
25
"relationships": {
26
"shipping_category": {
27
"links": {...}
28
},
29
"prices": {
30
"links": {...}
31
},
32
"stock_items": {
33
"links": {...}
34
},
35
"delivery_lead_times": {
36
"links": {...}
37
},
38
"sku_options": {
39
"links": {...}
40
}
41
},
42
"meta": {
43
"mode": "test",
44
"organization_id": "JxYabZKcAw"
45
}
46
},
47
{
48
"other": "... 9 skus (first page)"
49
}
50
],
51
"meta": {
52
"record_count": 140,
53
"page_count": 14
54
},
55
"links": {
56
"first": "https://yourdomain.commercelayer.io/api/skus?sort=code&page[number]=1&page[size]=10",
57
"next": "https://yourdomain.commercelayer.io/api/skus?sort=code&page[number]=2&page[size]=10",
58
"last": "https://yourdomain.commercelayer.io/api/skus?sort=code&page[number]=14&page[size]=10"
59
}
60
}
Copied!
Request
Response
The following request fetches the list of orders associated with a certain customer, sorted by their creation date (descending):
1
curl -g -X GET \
2
'https://yourdomain.commercelayer.io/api/customers/xYZkjABcde/orders?sort=-created_at' \
3
-H 'Accept: application/vnd.api+json' \
4
-H 'Authorization: Bearer your-access-token'
Copied!
On success, the API responds with a 200 OK status code, returning a paginated collection of the related resource objects, sorted in the requested order:
1
{
2
"data": [
3
{
4
"id": "kjVLxYzbAc",
5
"type": "orders",
6
"links": {...},
7
"attributes": {
8
"number": 1234,
9
"autorefresh": true,
10
"status": "pending",
11
"payment_status": "unpaid",
12
"fulfillment_status": "unfulfilled",
13
"guest": false,
14
"editable": true,
15
"customer_email": "[email protected]",
16
"language_code": "en",
17
"currency_code": "USD",
18
"tax_included": false,
19
"tax_rate": null,
20
"freight_taxable": null,
21
"requires_billing_info": false,
22
"country_code": "US",
23
"shipping_country_code_lock": null,
24
"coupon_code": null,
25
"gift_card_code": null,
26
"gift_card_or_coupon_code": null,
27
"subtotal_amount_cents": 3480,
28
"subtotal_amount_float": 34.8,
29
"formatted_subtotal_amount": "$34.80",
30
"shipping_amount_cents": 700,
31
"shipping_amount_float": 7.0,
32
"formatted_shipping_amount": "$7.00",
33
"payment_method_amount_cents": 0,
34
"payment_method_amount_float": 0.0,
35
"formatted_payment_method_amount": "$0.00",
36
"discount_amount_cents": 0,
37
"discount_amount_float": 0.0,
38
"formatted_discount_amount": "$0.00",
39
"adjustment_amount_cents": 0,
40
"adjustment_amount_float": 0.0,
41
"formatted_adjustment_amount": "$0.00",
42
"gift_card_amount_cents": 0,
43
"gift_card_amount_float": 0.0,
44
"formatted_gift_card_amount": "$0.00",
45
"total_tax_amount_cents": 0,
46
"total_tax_amount_float": 0.0,
47
"formatted_total_tax_amount": "$0.00",
48
"subtotal_tax_amount_cents": 0,
49
"subtotal_tax_amount_float": 0.0,
50
"formatted_subtotal_tax_amount": "$0.00",
51
"shipping_tax_amount_cents": 0,
52
"shipping_tax_amount_float": 0.0,
53
"formatted_shipping_tax_amount": "$0.00",
54
"payment_method_tax_amount_cents": 0,
55
"payment_method_tax_amount_float": 0.0,
56
"formatted_payment_method_tax_amount": "$0.00",
57
"adjustment_tax_amount_cents": 0,
58
"adjustment_tax_amount_float": 0.0,
59
"formatted_adjustment_tax_amount": "$0.00",
60
"total_amount_cents": 4180,
61
"total_amount_float": 41.8,
62
"formatted_total_amount": "$41.80",
63
"total_taxable_amount_cents": 4180,
64
"total_taxable_amount_float": 41.8,
65
"formatted_total_taxable_amount": "$41.80",
66
"subtotal_taxable_amount_cents": 3480,
67
"subtotal_taxable_amount_float": 34.8,
68
"formatted_subtotal_taxable_amount": "$34.80",
69
"shipping_taxable_amount_cents": 700,
70
"shipping_taxable_amount_float": 7.0,
71
"formatted_shipping_taxable_amount": "$7.00",
72
"payment_method_taxable_amount_cents": 0,
73
"payment_method_taxable_amount_float": 0.0,
74
"formatted_payment_method_taxable_amount": "$0.00",
75
"adjustment_taxable_amount_cents": 0,
76
"adjustment_taxable_amount_float": 0.0,
77
"formatted_adjustment_taxable_amount": "$0.00",
78
"total_amount_with_taxes_cents": 4180,
79
"total_amount_with_taxes_float": 41.8,
80
"formatted_total_amount_with_taxes": "$41.80",
81
"fees_amount_cents": 0,
82
"fees_amount_float": 0.0,
83
"formatted_fees_amount": "$0.00",
84
"duty_amount_cents": null,
85
"duty_amount_float": null,
86
"formatted_duty_amount": null,
87
"skus_count": 1,
88
"line_item_options_count": 0,
89
"shipments_count": 1,
90
"payment_source_details": null,
91
"token": "y0ur-t0K3n",
92
"cart_url": null,
93
"return_url": null,
94
"terms_url": null,
95
"privacy_url": null,
96
"checkout_url": "https://your-checkout-url/kjVLxYzbAc",
97
"placed_at": null,
98
"approved_at": null,
99
"cancelled_at": null,
100
"payment_updated_at": null,
101
"fulfillment_updated_at": null,
102
"refreshed_at": null,
103
"archived_at": null,
104
"expires_at": null,
105
"created_at": "2018-01-01T12:00:00.000Z",
106
"updated_at": "2018-01-01T12:00:00.000Z",
107
"reference": "",
108
"reference_origin": null,
109
"metadata": {}
110
},
111
"relationships": {
112
"market": {
113
"links": {...}
114
},
115
"customer": {
116
"links": {...}
117
},
118
"shipping_address": {
119
"links": {...}
120
},
121
"billing_address": {
122
"links": {...}
123
},
124
"available_payment_methods": {
125
"links": {...}
126
},
127
"available_customer_payment_sources": {
128
"links": {...}
129
},
130
"payment_method": {
131
"links": {...}
132
},
133
"payment_source": {
134
"links": {...}
135
},
136
"line_items": {
137
"links": {...}
138
},
139
"shipments": {
140
"links": {...}
141
},
142
"transactions": {
143
"links": {...}
144
},
145
"authorizations": {
146
"links": {...}
147
},
148
"captures": {
149
"links": {...}
150
},
151
"voids": {
152
"links": {...}
153
},
154
"refunds": {
155
"links": {...}
156
},
157
"order_subscriptions": {
158
"links": {...}
159
},
160
"order_copies": {
161
"links": {...}
162
},
163
"attachments": {
164
"links": {...}
165
}
166
},
167
"meta": {
168
"mode": "test",
169
"organization_id": "JxYabZKcAw"
170
}
171
},
172
{
173
"other": "... 9 orders (first page)"
174
}
175
],
176
"meta": {
177
"record_count": 70,
178
"page_count": 7
179
},
180
"links": {
181
"first": "https://yourdomain.commercelayer.io/api/customers/xYZkjABcde/orders?sort=-created_at&page[number]=1&page[size]=10",
182
"next": "https://yourdomain.commercelayer.io/api/customers/xYZkjABcde/orders?sort=-created_at&page[number]=2&page[size]=10",
183
"last": "https://yourdomain.commercelayer.io/api/customers/xYZkjABcde/orders?sort=-created_at&page[number]=7&page[size]=10"
184
}
185
}
Copied!
You can get the full list of sortable attributes from the documentation of each resource.
Copy link
Contents
Examples