Bird
Raised Fist0
Rest APIprogramming~20 mins

Sort direction (asc, desc) in Rest API - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Sort Direction Master
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 query with ascending sort?

Consider a REST API endpoint that returns a list of users sorted by age. The query parameter sort=age:asc is used.

Given the following user ages in the database: [34, 21, 45, 29], what order of ages will the API return?

A[34, 21, 45, 29]
B[45, 34, 29, 21]
C[21, 29, 34, 45]
D[29, 21, 34, 45]
Attempts:
2 left
💡 Hint

Ascending means from smallest to largest.

❓ Predict Output
intermediate
2:00remaining
What is the output of this REST API query with descending sort?

Using the same user ages [34, 21, 45, 29], what order will the API return if the query parameter is sort=age:desc?

A[45, 34, 29, 21]
B[34, 21, 45, 29]
C[29, 21, 34, 45]
D[21, 29, 34, 45]
Attempts:
2 left
💡 Hint

Descending means from largest to smallest.

🧠 Conceptual
advanced
2:00remaining
Which query parameter correctly sorts by name in ascending order?

You want to get a list of users sorted by their name in ascending order using a REST API. Which query parameter is correct?

Asort=asc:name
Bsort=name:asc
Corder=name:asc
Dsort=ascending:name
Attempts:
2 left
💡 Hint

Look for the format sort=field:direction.

❓ Predict Output
advanced
2:00remaining
What error occurs with an invalid sort direction?

If a REST API receives the query parameter sort=age:upwards, what is the most likely response?

A500 Internal Server Error
B200 OK with unsorted results
C200 OK with results sorted ascending by default
D400 Bad Request with error message about invalid sort direction
Attempts:
2 left
💡 Hint

APIs usually validate query parameters and reject invalid values.

🚀 Application
expert
2:00remaining
How many items are returned when sorting with pagination?

A REST API returns a list of 100 users sorted by age descending. The query parameters are sort=age:desc and limit=10. How many users will be in the response?

A10
B100
C90
DDepends on the API default
Attempts:
2 left
💡 Hint

The limit parameter controls how many items are returned.

Practice

(1/5)
1.

What does the direction=asc parameter do in a REST API request?

easy
A. Sorts the data in ascending order, from smallest to largest.
B. Sorts the data in descending order, from largest to smallest.
C. Filters the data to show only ascending values.
D. Deletes all data except the first item.

Solution

  1. Step 1: Understand the meaning of 'asc'

    'asc' stands for ascending, which means sorting from smallest to largest.
  2. Step 2: Apply to REST API sorting

    When used as a direction parameter, it orders the data from smallest to largest values.
  3. Final Answer:

    Sorts the data in ascending order, from smallest to largest. -> Option A
  4. Quick Check:

    direction=asc means ascending order [OK]
Hint: Asc means smallest to largest order in sorting [OK]
Common Mistakes:
  • Confusing 'asc' with descending order
  • Thinking 'asc' filters data instead of sorting
  • Assuming 'asc' deletes data
2.

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&_____
easy
A. direction=asc
B. order=asc
C. sort=desc
D. direction=desc

Solution

  1. Step 1: Identify the parameter for sort direction

    The common parameter to specify sort direction is 'direction'.
  2. Step 2: Choose the correct value for descending order

    Descending order is indicated by 'desc'. So the correct query is 'direction=desc'.
  3. Final Answer:

    direction=desc -> Option D
  4. Quick Check:

    Descending sort uses direction=desc [OK]
Hint: Use direction=desc for descending sort in query [OK]
Common Mistakes:
  • Using 'sort=desc' instead of 'direction=desc'
  • Mixing 'asc' and 'desc' values
  • Using 'order=asc' which is not standard here
3.

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}
]
medium
A. 7.9
B. 8.7
C. 9.5
D. Cannot determine

Solution

  1. Step 1: Understand descending order sorting

    Descending order means highest values come first.
  2. Step 2: Check the first item in the response

    The first item has rating 9.5, which is the highest rating.
  3. Final Answer:

    9.5 -> Option C
  4. Quick Check:

    Descending sort puts 9.5 first [OK]
Hint: Descending means highest value is first in list [OK]
Common Mistakes:
  • Picking the last item's rating instead of first
  • Confusing ascending with descending order
  • Choosing 'Cannot determine' without checking response
4.

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
medium
A. The parameter 'direction' should be 'desc' not 'descending'.
B. The 'sort' parameter should be 'desc' instead of 'age'.
C. The URL is missing the 'order' parameter.
D. The '&' symbol should be replaced with '?' in the URL.

Solution

  1. Step 1: Check the 'direction' parameter value

    The correct values for direction are 'asc' or 'desc', not 'descending'.
  2. Step 2: Confirm the 'sort' parameter is correct

    'sort=age' is correct to specify sorting by age.
  3. Final Answer:

    The parameter 'direction' should be 'desc' not 'descending'. -> Option A
  4. Quick Check:

    Use 'desc' for descending direction [OK]
Hint: Use 'desc' not 'descending' for direction [OK]
Common Mistakes:
  • Using full word 'descending' instead of 'desc'
  • Changing 'sort' parameter incorrectly
  • Misplacing '&' and '?' in URL
5.

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?

hard
A. sort=price&direction=desc&filter=price>100
B. filter=price>100&sort=price&direction=asc
C. sort=price&direction=asc&filter=price=100
D. direction=asc&filter=price>100

Solution

  1. Step 1: Identify sorting parameters

    Sorting by price ascending requires 'sort=price' and 'direction=asc'.
  2. 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.
  3. Final Answer:

    filter=price>100&sort=price&direction=asc -> Option B
  4. Quick Check:

    Filter then sort ascending with correct parameters [OK]
Hint: Combine filter and sort with correct parameters [OK]
Common Mistakes:
  • Using descending direction instead of ascending
  • Omitting filter parameter
  • Incorrect parameter order causing confusion
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