Bird
Raised Fist0
Rest APIprogramming~10 mins

Sort direction (asc, desc) in Rest API - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Sort direction (asc, desc)
Receive API request with sort param
↓
Check if sort param is 'desc'
↓
Sort data descending
↓
Return sorted data in response
↓
END
The API receives a request with a sort direction parameter, checks if it is ascending or descending, sorts the data accordingly, and returns the sorted data.
Execution Sample
Rest API
GET /items?sort=asc
// Server sorts items ascending
// Returns sorted list
This API call sorts items in ascending order and returns them.
Execution Table
StepInput sort paramConditionSort DirectionActionOutput
1ascsort == 'desc'?NoSort ascending[1, 2, 3, 4, 5]
2descsort == 'desc'?YesSort descending[5, 4, 3, 2, 1]
3nonesort == 'desc'?NoDefault sort ascending[1, 2, 3, 4, 5]
💡 Sorting completes and sorted data is returned in response
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3
sort_paramnullascdescnone
sorted_data[][1, 2, 3, 4, 5][5, 4, 3, 2, 1][1, 2, 3, 4, 5]
Key Moments - 2 Insights
What happens if the sort parameter is missing or invalid?
The code defaults to ascending sort as shown in step 3 of the execution_table.
Why do we check if sort == 'desc' first?
Because if it's 'desc', we sort descending; otherwise, we sort ascending or default, as shown in steps 1 and 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the sorted_data output when sort_param is 'desc'?
A[5, 4, 3, 2, 1]
B[1, 2, 3, 4, 5]
C[]
DError
💡 Hint
Check row 2 under Output column in execution_table
At which step does the code default to ascending sort?
AStep 1
BStep 2
CStep 3
DNo default
💡 Hint
Look at the Condition and Action columns in execution_table row 3
If the input sort_param changes from 'asc' to 'none', how does sorted_data change?
AFrom descending to ascending
BFrom ascending to ascending
CFrom ascending to descending
DNo change
💡 Hint
Compare sorted_data values in variable_tracker for After Step 1 and After Step 3
Concept Snapshot
Sort direction controls order of data returned by API.
Use 'asc' for ascending, 'desc' for descending.
If missing or invalid, default to ascending.
Check sort param, then sort data accordingly.
Return sorted data in API response.
Full Transcript
This visual execution shows how a REST API handles a sort direction parameter. The API receives a request with a sort parameter that can be 'asc' or 'desc'. It checks the parameter: if it is 'asc', it sorts data in ascending order; if 'desc', it sorts descending. If the parameter is missing or invalid, it defaults to ascending sort. The sorted data is then returned in the response. The execution table traces three cases: 'asc', 'desc', and missing parameter. The variable tracker shows how the sort_param and sorted_data variables change. Key moments clarify default behavior and condition checks. The quiz tests understanding of outputs and default sorting. This helps beginners see step-by-step how sort direction affects API data sorting.

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