0
0
Rest APIprogramming~10 mins

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

Choose your learning style9 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.