Sorting with sort parameter in REST API
📖 Scenario: You are building a simple REST API that returns a list of products. Users want to get the products sorted by price or name.
🎯 Goal: Create a REST API endpoint that returns a list of products sorted by a query parameter called sort. The sort parameter can be either price or name. If no sort parameter is given, return the products unsorted.
📋 What You'll Learn
Create a list of products with exact entries
Add a variable to read the
sort parameterSort the products list by the
sort parameter if it is price or nameReturn the sorted or unsorted list as JSON
💡 Why This Matters
🌍 Real World
Sorting data by user preference is common in online stores and APIs to improve user experience.
💼 Career
Understanding how to handle query parameters and sort data is essential for backend developers working with REST APIs.
Progress0 / 4 steps