Bird
Raised Fist0
Rest APIprogramming~15 mins

Sort direction (asc, desc) in Rest API - Mini Project: Build & Apply

Choose your learning style10 modes available
Sort Direction (asc, desc) in REST API
📖 Scenario: You are building a simple REST API that returns a list of products. Users can choose to get the products sorted by price in ascending or descending order.
🎯 Goal: Create a small Python program that simulates sorting product data by price in ascending or descending order based on a configuration variable.
📋 What You'll Learn
Create a dictionary called products with product names as keys and prices as values.
Create a variable called sort_direction that can be either 'asc' or 'desc'.
Use a sorting method that sorts the products by price according to the sort_direction.
Print the sorted list of product names and prices.
💡 Why This Matters
🌍 Real World
Sorting data by ascending or descending order is common in APIs that return lists of items, like products, users, or events.
💼 Career
Understanding how to control sort direction helps you build flexible APIs and user interfaces that show data in the order users want.
Progress0 / 4 steps
1
Create the product data
Create a dictionary called products with these exact entries: 'apple': 120, 'banana': 80, 'cherry': 150, 'date': 100.
Rest API
Hint

Use curly braces {} to create a dictionary with keys and values separated by colons.

2
Set the sort direction
Create a variable called sort_direction and set it to the string 'asc' to sort prices in ascending order.
Rest API
Hint

Assign the string 'asc' to the variable sort_direction.

3
Sort the products by price
Create a variable called sorted_products that sorts the items of products by price. Use sort_direction to decide ascending or descending order. Use sorted(products.items(), key=lambda item: item[1], reverse=sort_direction == 'desc').
Rest API
Hint

Use the sorted() function with key=lambda item: item[1] to sort by price. Set reverse=True if sort_direction is 'desc'.

4
Print the sorted products
Print the sorted_products list to show the product names and prices in the chosen order using print(sorted_products).
Rest API
Hint

Use print() to show the sorted list.

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