Sort direction (asc, desc) in Rest API - Time & Space Complexity
When sorting data in ascending or descending order, it is important to understand how the time taken grows as the data size increases.
We want to know how the sorting operation's cost changes when we have more items to sort.
Analyze the time complexity of the following code snippet.
GET /items?sort=asc
// Server fetches all items
// Then sorts items by price ascending or descending
const sortedItems = items.sort((a, b) => {
if (sortDirection === 'asc') return a.price - b.price;
else return b.price - a.price;
});
return sortedItems;
This code sorts a list of items by their price in either ascending or descending order based on the request.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The sorting algorithm compares pairs of items multiple times.
- How many times: The number of comparisons grows as the list size increases, usually more than once per item.
As the number of items grows, the sorting takes more time because it must compare many pairs.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 30 comparisons |
| 100 | About 700 comparisons |
| 1000 | About 10,000 comparisons |
Pattern observation: The number of operations grows faster than the number of items, roughly as n log n.
Time Complexity: O(n log n)
This means that as the list grows, the time to sort grows a bit faster than the list size but much slower than checking every pair.
[X] Wrong: "Sorting ascending is faster than sorting descending because it feels simpler."
[OK] Correct: Both ascending and descending sorting use the same process and take about the same time regardless of direction.
Understanding how sorting time grows helps you explain performance in real apps where users want data sorted quickly.
"What if the items were already sorted? How would that affect the time complexity of sorting in ascending or descending order?"
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
