Sorting helps organize data so it's easier to find or understand. Using ascending (asc) or descending (desc) order tells the system how to arrange the data.
Sort direction (asc, desc) in Rest API
GET /items?sort=field_name&direction=asc GET /items?sort=field_name&direction=desc
sort specifies which field to sort by.
direction can be asc for ascending or desc for descending order.
GET /users?sort=age&direction=asc
GET /products?sort=price&direction=desc
GET /books?sort=title&direction=asc
This program shows how to request sorted data from an API by specifying the sort field and direction. It prints the full URL used for each request.
import requests # Example API endpoint url = 'https://api.example.com/items' # Sort items by 'name' in ascending order params_asc = {'sort': 'name', 'direction': 'asc'} response_asc = requests.get(url, params=params_asc) # Sort items by 'name' in descending order params_desc = {'sort': 'name', 'direction': 'desc'} response_desc = requests.get(url, params=params_desc) print('Ascending order request URL:', response_asc.url) print('Descending order request URL:', response_desc.url)
Not all APIs support sorting; check the API documentation first.
If you omit the direction, some APIs default to ascending order.
Sorting large data sets may slow down the response time.
Sort direction controls how data is ordered: ascending (asc) or descending (desc).
Use query parameters like sort and direction in API requests to specify sorting.
Sorting makes data easier to read and find.
Practice
What does the direction=asc parameter do in a REST API request?
Solution
Step 1: Understand the meaning of 'asc'
'asc' stands for ascending, which means sorting from smallest to largest.Step 2: Apply to REST API sorting
When used as a direction parameter, it orders the data from smallest to largest values.Final Answer:
Sorts the data in ascending order, from smallest to largest. -> Option AQuick Check:
direction=asc means ascending order [OK]
- Confusing 'asc' with descending order
- Thinking 'asc' filters data instead of sorting
- Assuming 'asc' deletes data
Which of the following is the correct way to specify descending sort direction in a REST API URL query?
https://api.example.com/items?sort=price&_____
Solution
Step 1: Identify the parameter for sort direction
The common parameter to specify sort direction is 'direction'.Step 2: Choose the correct value for descending order
Descending order is indicated by 'desc'. So the correct query is 'direction=desc'.Final Answer:
direction=desc -> Option DQuick Check:
Descending sort uses direction=desc [OK]
- Using 'sort=desc' instead of 'direction=desc'
- Mixing 'asc' and 'desc' values
- Using 'order=asc' which is not standard here
Given this REST API response sorted by rating in descending order, what is the first item's rating?
GET https://api.example.com/movies?sort=rating&direction=desc
Response:
[
{"title": "Movie A", "rating": 9.5},
{"title": "Movie B", "rating": 8.7},
{"title": "Movie C", "rating": 7.9}
]Solution
Step 1: Understand descending order sorting
Descending order means highest values come first.Step 2: Check the first item in the response
The first item has rating 9.5, which is the highest rating.Final Answer:
9.5 -> Option CQuick Check:
Descending sort puts 9.5 first [OK]
- Picking the last item's rating instead of first
- Confusing ascending with descending order
- Choosing 'Cannot determine' without checking response
Identify the error in this REST API request URL that tries to sort users by age in descending order:
https://api.example.com/users?sort=age&direction=descending
Solution
Step 1: Check the 'direction' parameter value
The correct values for direction are 'asc' or 'desc', not 'descending'.Step 2: Confirm the 'sort' parameter is correct
'sort=age' is correct to specify sorting by age.Final Answer:
The parameter 'direction' should be 'desc' not 'descending'. -> Option AQuick Check:
Use 'desc' for descending direction [OK]
- Using full word 'descending' instead of 'desc'
- Changing 'sort' parameter incorrectly
- Misplacing '&' and '?' in URL
You want to get a list of products sorted by price ascending, but only those with price greater than 100. Which query parameters correctly combine sorting and filtering?
Solution
Step 1: Identify sorting parameters
Sorting by price ascending requires 'sort=price' and 'direction=asc'.Step 2: Add filtering for price greater than 100
Filtering is done with 'filter=price>100'. The order of parameters does not affect functionality but including all is best practice.Final Answer:
filter=price>100&sort=price&direction=asc -> Option BQuick Check:
Filter then sort ascending with correct parameters [OK]
- Using descending direction instead of ascending
- Omitting filter parameter
- Incorrect parameter order causing confusion
Ready to prove it?
Pass the test and this pattern moves unknown
