Sorting with sort parameter in Rest API - Time & Space Complexity
When using a sort parameter in a REST API, the server arranges data before sending it. Understanding how this sorting affects time helps us know how fast the API responds as data grows.
We want to see how the time to sort changes when the number of items increases.
Analyze the time complexity of the following code snippet.
GET /items?sort=price
// Server receives request
// Fetch all items from database
// Sort items by price
// Return sorted list
This code fetches a list of items and sorts them by price before sending the response.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Sorting the list of items by price.
- How many times: The sorting algorithm compares and rearranges items multiple times depending on the number of items.
As the number of items grows, the sorting work grows faster than just counting items. For example:
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 30 comparisons |
| 100 | About 700 comparisons |
| 1000 | About 10,000 comparisons |
Pattern observation: When the list size grows, the number of operations grows faster than the list size itself, roughly multiplying by n log n.
Time Complexity: O(n log n)
This means sorting takes more time as the list grows, but not as fast as checking every pair; it grows in a balanced way with the number of items.
[X] Wrong: "Sorting with a sort parameter takes the same time no matter how many items there are."
[OK] Correct: Sorting needs to compare and arrange items, so more items mean more work and longer time.
Knowing how sorting scales helps you explain API performance clearly. It shows you understand how data size affects response time, a useful skill in real projects.
"What if the API returned only the top 10 sorted items instead of all? How would the time complexity change?"
Practice
sort parameter do in a REST API request?Solution
Step 1: Understand the purpose of the
Thesortparametersortparameter is used to arrange the returned data in a specific order.Step 2: Differentiate from other parameters
Filtering limits data, limiting controls count, and format changes output type, but sorting changes order.Final Answer:
It controls the order in which data is returned. -> Option BQuick Check:
Sort parameter = controls order [OK]
- Confusing sort with filter
- Thinking sort limits results
- Assuming sort changes data format
price in descending order using the sort parameter?Solution
Step 1: Identify descending order syntax
In REST APIs, prefixing the field with a minus sign-sorts in descending order.Step 2: Match the correct syntax
Onlysort=-pricecorrectly uses the minus prefix before the field name.Final Answer:
sort=-price -> Option CQuick Check:
Minus before field = descending sort [OK]
- Placing '-' after the field name
- Using function-like syntax incorrectly
- Appending '_desc' instead of '-' prefix
/products?sort=-rating, what will be the order of the returned products?Solution
Step 1: Analyze the sort parameter
The parametersort=-ratingmeans sort by the fieldratingin descending order.Step 2: Determine the order of sorting
Descending order means highest values first, so products with highest rating come first.Final Answer:
Products sorted by rating from highest to lowest. -> Option AQuick Check:
Minus prefix = descending order [OK]
- Assuming minus means ascending
- Confusing rating with price or name
- Ignoring the minus sign
/users?sort=name- to sort users by name descending, but it returns an error. What is the problem?Solution
Step 1: Check the position of the minus sign
The correct syntax places the minus sign before the field name to indicate descending order.Step 2: Identify the error cause
Usingname-places the minus after the field, which is invalid and causes an error.Final Answer:
The minus sign should be before the field name, not after. -> Option AQuick Check:
Minus must prefix field for descending [OK]
- Placing minus after field name
- Assuming sort can't be used on strings
- Using wrong parameter like 'order'
date descending, then by title ascending. Which sort parameter should you use?Solution
Step 1: Understand multi-field sorting syntax
Multiple fields are separated by commas; order matters for sorting priority.Step 2: Apply descending and ascending correctly
Use-datefor descending date, andtitlewithout minus for ascending title.Step 3: Combine fields in correct order
First sort by date descending, then by title ascending meanssort=-date,title.Final Answer:
sort=-date,title -> Option DQuick Check:
Minus before date, comma, then title ascending [OK]
- Reversing field order
- Putting minus on ascending field
- Using wrong separators like semicolon
Ready to prove it?
Pass the test and this pattern moves unknown
