What is the purpose of the sort parameter in a REST API?
The sort parameter tells the API how to order the results it sends back, like sorting a list of names alphabetically or by date.
Click to reveal answer
beginner
How do you specify ascending or descending order using the sort parameter?
You usually add a plus + or minus - sign before the field name. For example, sort=+name means ascending by name, and sort=-date means descending by date.
Click to reveal answer
intermediate
Can you sort by multiple fields using the sort parameter? How?
Yes! You separate fields with commas. For example, sort=+category,-price sorts first by category ascending, then by price descending within each category.
Click to reveal answer
beginner
Why is sorting important in API responses?
Sorting helps users find information faster and makes data easier to understand, like sorting your contacts by last name to find someone quickly.
Click to reveal answer
beginner
What happens if you don't use a sort parameter in your API request?
The API may return data in a default order, which might be random or based on how data is stored. This can make it harder to find what you want.
Click to reveal answer
What does sort=-createdAt usually mean in an API request?
ASort results alphabetically by 'createdAt'
BSort results by creation date ascending (oldest first)
CSort results by creation date descending (newest first)
DFilter results created after a certain date
✗ Incorrect
A minus sign before the field means descending order, so newest items come first.
How do you sort by two fields, first by 'name' ascending, then by 'age' descending?
Asort=name,age
Bsort=age,name
Csort=-name,+age
Dsort=+name,-age
✗ Incorrect
Use plus for ascending and minus for descending, separated by commas.
If no sort parameter is given, what is likely true?
AResults are sorted by the API's default order
BResults are always sorted alphabetically
CResults are sorted by the user's last request
DResults are filtered instead of sorted
✗ Incorrect
APIs usually have a default order if no sorting is specified.
Which of these is a valid way to sort descending by price?
Asort=-price
Bsort=desc(price)
Csort=price-
Dsort=price_desc
✗ Incorrect
A minus sign before the field name indicates descending order.
Why might an API support sorting by multiple fields?
ATo speed up the API response time
BTo allow complex ordering like sorting by category then price
CTo filter out unwanted data
DTo encrypt the data
✗ Incorrect
Sorting by multiple fields helps organize data in layers, like sorting books by genre then author.
Explain how the sort parameter works in a REST API and why it is useful.
Think about how you organize lists in daily life.
You got /4 concepts.
Describe how to sort API results by multiple fields and give an example.
Imagine sorting a list of products by category and then price.
You got /4 concepts.
Practice
(1/5)
1. What does the sort parameter do in a REST API request?
easy
A. It filters data based on a condition.
B. It controls the order in which data is returned.
C. It limits the number of results returned.
D. It changes the data format to XML.
Solution
Step 1: Understand the purpose of the sort parameter
The sort parameter 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 B
Quick Check:
Sort parameter = controls order [OK]
Hint: Sort means order, not filter or limit [OK]
Common Mistakes:
Confusing sort with filter
Thinking sort limits results
Assuming sort changes data format
2. Which of the following is the correct way to sort results by the field price in descending order using the sort parameter?
easy
A. sort=desc(price)
B. sort=price-
C. sort=-price
D. sort=price_desc
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
Only sort=-price correctly uses the minus prefix before the field name.
Final Answer:
sort=-price -> Option C
Quick Check:
Minus before field = descending sort [OK]
Hint: Use '-' before field for descending sort [OK]
Common Mistakes:
Placing '-' after the field name
Using function-like syntax incorrectly
Appending '_desc' instead of '-' prefix
3. Given the API request /products?sort=-rating, what will be the order of the returned products?
medium
A. Products sorted by rating from highest to lowest.
B. Products sorted alphabetically by name.
C. Products sorted by rating from lowest to highest.
D. Products sorted by price from lowest to highest.
Solution
Step 1: Analyze the sort parameter
The parameter sort=-rating means sort by the field rating in 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 A
Quick Check:
Minus prefix = descending order [OK]
Hint: Minus means descending, so highest values first [OK]
Common Mistakes:
Assuming minus means ascending
Confusing rating with price or name
Ignoring the minus sign
4. You wrote the API request /users?sort=name- to sort users by name descending, but it returns an error. What is the problem?
medium
A. The minus sign should be before the field name, not after.
B. The field name name is invalid for sorting.
C. The sort parameter cannot be used with strings.
D. You need to use order=name_desc instead.
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
Using name- 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 A
Quick Check:
Minus must prefix field for descending [OK]
Hint: Minus after field causes error; put it before [OK]
Common Mistakes:
Placing minus after field name
Assuming sort can't be used on strings
Using wrong parameter like 'order'
5. You want to get a list of blog posts sorted first by date descending, then by title ascending. Which sort parameter should you use?
hard
A. sort=-title,date
B. sort=date,-title
C. sort=title,-date
D. sort=-date,title
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 -date for descending date, and title without minus for ascending title.
Step 3: Combine fields in correct order
First sort by date descending, then by title ascending means sort=-date,title.
Final Answer:
sort=-date,title -> Option D
Quick Check:
Minus before date, comma, then title ascending [OK]
Hint: List fields comma-separated; minus means descending [OK]
Common Mistakes:
Reversing field order
Putting minus on ascending field
Using wrong separators like semicolon
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.