0
0
Rest APIprogramming~20 mins

Sorting with sort parameter in Rest API - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Sorting Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this REST API call with sort parameter?

Consider an API endpoint that returns a list of users sorted by age ascending when called with ?sort=age. What will be the order of user IDs in the response?

Rest API
GET /users?sort=age

Response JSON:
[
  {"id": 3, "name": "Alice", "age": 22},
  {"id": 1, "name": "Bob", "age": 25},
  {"id": 2, "name": "Charlie", "age": 30}
]
A[3, 1, 2]
B[1, 2, 3]
C[2, 1, 3]
D[3, 2, 1]
Attempts:
2 left
💡 Hint

Sorting by age ascending means the youngest user comes first.

🧠 Conceptual
intermediate
2:00remaining
Which sort parameter value sorts results by name descending?

You want to get a list of products sorted by their name in reverse alphabetical order using a REST API. Which sort parameter value should you use?

Asort=name_desc
Bsort=-name
Csort=name_reverse
Dsort=desc_name
Attempts:
2 left
💡 Hint

Many APIs use a minus sign before the field name to indicate descending order.

🔧 Debug
advanced
2:00remaining
Why does this API call with sort parameter cause an error?

An API call GET /items?sort=price,desc returns a 400 Bad Request error. What is the likely cause?

AThe API expects a single field without comma, not 'price,desc'.
BThe sort parameter should be 'sort=price-desc' instead of comma.
CThe API requires 'sort=desc:price' format.
DThe API does not support sorting by price.
Attempts:
2 left
💡 Hint

Check the API documentation for the expected format of the sort parameter.

Predict Output
advanced
2:00remaining
What is the output order of this API call with multiple sort fields?

An API supports sorting by multiple fields separated by commas. Given the call GET /books?sort=author,title, what is the order of books?

Rest API
Books data:
[
  {"author": "Smith", "title": "Zebra"},
  {"author": "Smith", "title": "Apple"},
  {"author": "Adams", "title": "Banana"}
]

Sorted by author ascending, then title ascending.
A[{"author": "Smith", "title": "Zebra"}, {"author": "Smith", "title": "Apple"}, {"author": "Adams", "title": "Banana"}]
B[{"author": "Smith", "title": "Apple"}, {"author": "Smith", "title": "Zebra"}, {"author": "Adams", "title": "Banana"}]
C[{"author": "Adams", "title": "Banana"}, {"author": "Smith", "title": "Apple"}, {"author": "Smith", "title": "Zebra"}]
D[{"author": "Adams", "title": "Banana"}, {"author": "Smith", "title": "Zebra"}, {"author": "Smith", "title": "Apple"}]
Attempts:
2 left
💡 Hint

Sorting by multiple fields means sorting by the first field, then by the second field within ties.

🚀 Application
expert
3:00remaining
How to implement sorting with a sort parameter in a REST API?

You are building a REST API that returns a list of products. You want to add a sort query parameter that supports sorting by one or more fields, each optionally descending with a minus sign prefix. Which approach correctly implements this?

AOnly allow sorting by one field without descending option, reject requests with multiple fields or minus sign.
BIgnore the sort parameter and always return products sorted by ID ascending.
CSort products randomly regardless of the sort parameter to improve performance.
DParse the sort parameter by splitting on commas, then for each field check if it starts with '-', sort descending by that field, else ascending.
Attempts:
2 left
💡 Hint

Think about how to handle multiple fields and descending order indicated by a minus sign.