Bird
Raised Fist0
Rest APIprogramming~10 mins

Sort direction (asc, desc) in Rest API - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to set the sort direction to ascending.

Rest API
params = {"sort": "name", "direction": "[1]"}
Drag options to blanks, or click blank then click option'
Adesc
Bdown
Cup
Dasc
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'desc' instead of 'asc' for ascending order.
2fill in blank
medium

Complete the code to set the sort direction to descending.

Rest API
params = {"sort": "date", "direction": "[1]"}
Drag options to blanks, or click blank then click option'
Aup
Basc
Cdesc
Ddown
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'asc' instead of 'desc' for descending order.
3fill in blank
hard

Fix the error in the sort direction value to make it valid.

Rest API
params = {"sort": "price", "direction": "[1]"}
Drag options to blanks, or click blank then click option'
Aupward
Bdesc
Cdownward
Dascending
Attempts:
3 left
💡 Hint
Common Mistakes
Using full words instead of 'asc' or 'desc'.
4fill in blank
hard

Fill both blanks to create a query with ascending sort on 'title'.

Rest API
params = {"sort": "[1]", "direction": "[2]"}
Drag options to blanks, or click blank then click option'
Atitle
Basc
Cdesc
Ddate
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up sort field and direction values.
5fill in blank
hard

Fill all three blanks to create a query sorting by 'rating' descending.

Rest API
params = {"sort": "[1]", "direction": "[2]", "limit": [3]
Drag options to blanks, or click blank then click option'
Adate
Bdesc
C10
Drating
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong field or direction, or putting limit as a string.

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