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