Bird
Raised Fist0
Rest APIprogramming~20 mins

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

Choose your learning style10 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.

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

  1. Step 1: Understand the purpose of the sort parameter

    The sort parameter is used to arrange the returned data in a specific order.
  2. Step 2: Differentiate from other parameters

    Filtering limits data, limiting controls count, and format changes output type, but sorting changes order.
  3. Final Answer:

    It controls the order in which data is returned. -> Option B
  4. 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

  1. Step 1: Identify descending order syntax

    In REST APIs, prefixing the field with a minus sign - sorts in descending order.
  2. Step 2: Match the correct syntax

    Only sort=-price correctly uses the minus prefix before the field name.
  3. Final Answer:

    sort=-price -> Option C
  4. 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

  1. Step 1: Analyze the sort parameter

    The parameter sort=-rating means sort by the field rating in descending order.
  2. Step 2: Determine the order of sorting

    Descending order means highest values first, so products with highest rating come first.
  3. Final Answer:

    Products sorted by rating from highest to lowest. -> Option A
  4. 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

  1. Step 1: Check the position of the minus sign

    The correct syntax places the minus sign before the field name to indicate descending order.
  2. Step 2: Identify the error cause

    Using name- places the minus after the field, which is invalid and causes an error.
  3. Final Answer:

    The minus sign should be before the field name, not after. -> Option A
  4. 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

  1. Step 1: Understand multi-field sorting syntax

    Multiple fields are separated by commas; order matters for sorting priority.
  2. Step 2: Apply descending and ascending correctly

    Use -date for descending date, and title without minus for ascending title.
  3. Step 3: Combine fields in correct order

    First sort by date descending, then by title ascending means sort=-date,title.
  4. Final Answer:

    sort=-date,title -> Option D
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard