Sorting with a sort parameter helps you get data in the order you want, like from smallest to largest or newest to oldest.
Sorting with sort parameter in Rest API
GET /items?sort=field_name GET /items?sort=-field_name
The sort parameter is added to the URL query string.
Use a minus sign (-) before the field name to sort in descending order.
GET /products?sort=price
GET /posts?sort=-date
GET /users?sort=name
This program calls an API to get items sorted by rating from highest to lowest. It prints each item's name and rating.
import requests # Example API endpoint url = 'https://api.example.com/items' # Sort items by 'rating' descending params = {'sort': '-rating'} response = requests.get(url, params=params) if response.status_code == 200: items = response.json() for item in items: print(f"{item['name']}: {item['rating']}") else: print('Failed to get items')
Not all APIs support sorting with a sort parameter; check the API documentation.
Sorting fields must be valid fields in the data, or the API may return an error or ignore the parameter.
Descending order is usually indicated by a minus sign before the field name.
Use the sort parameter in API requests to control the order of returned data.
Prefix the field with - to sort in descending order.
Sorting makes data easier to understand and use in your app or website.
Practice
sort parameter do in a REST API request?Solution
Step 1: Understand the purpose of the
Thesortparametersortparameter is used to arrange the returned data in a specific order.Step 2: Differentiate from other parameters
Filtering limits data, limiting controls count, and format changes output type, but sorting changes order.Final Answer:
It controls the order in which data is returned. -> Option BQuick Check:
Sort parameter = controls order [OK]
- Confusing sort with filter
- Thinking sort limits results
- Assuming sort changes data format
price in descending order using the sort parameter?Solution
Step 1: Identify descending order syntax
In REST APIs, prefixing the field with a minus sign-sorts in descending order.Step 2: Match the correct syntax
Onlysort=-pricecorrectly uses the minus prefix before the field name.Final Answer:
sort=-price -> Option CQuick Check:
Minus before field = descending sort [OK]
- Placing '-' after the field name
- Using function-like syntax incorrectly
- Appending '_desc' instead of '-' prefix
/products?sort=-rating, what will be the order of the returned products?Solution
Step 1: Analyze the sort parameter
The parametersort=-ratingmeans sort by the fieldratingin descending order.Step 2: Determine the order of sorting
Descending order means highest values first, so products with highest rating come first.Final Answer:
Products sorted by rating from highest to lowest. -> Option AQuick Check:
Minus prefix = descending order [OK]
- Assuming minus means ascending
- Confusing rating with price or name
- Ignoring the minus sign
/users?sort=name- to sort users by name descending, but it returns an error. What is the problem?Solution
Step 1: Check the position of the minus sign
The correct syntax places the minus sign before the field name to indicate descending order.Step 2: Identify the error cause
Usingname-places the minus after the field, which is invalid and causes an error.Final Answer:
The minus sign should be before the field name, not after. -> Option AQuick Check:
Minus must prefix field for descending [OK]
- Placing minus after field name
- Assuming sort can't be used on strings
- Using wrong parameter like 'order'
date descending, then by title ascending. Which sort parameter should you use?Solution
Step 1: Understand multi-field sorting syntax
Multiple fields are separated by commas; order matters for sorting priority.Step 2: Apply descending and ascending correctly
Use-datefor descending date, andtitlewithout minus for ascending title.Step 3: Combine fields in correct order
First sort by date descending, then by title ascending meanssort=-date,title.Final Answer:
sort=-date,title -> Option DQuick Check:
Minus before date, comma, then title ascending [OK]
- Reversing field order
- Putting minus on ascending field
- Using wrong separators like semicolon
Ready to prove it?
Pass the test and this pattern moves unknown
