Sorting arguments in GraphQL - Time & Space Complexity
When we use sorting arguments in GraphQL queries, the system arranges data in a certain order before giving results.
We want to know how the time to get sorted results grows as the data size grows.
Analyze the time complexity of the following GraphQL query with sorting.
query GetUsersSorted {
users(orderBy: {field: "name", direction: ASC}) {
id
name
email
}
}
This query fetches a list of users sorted by their name in ascending order.
Look for repeated steps that take time as data grows.
- Primary operation: Sorting the list of users by their name.
- How many times: The sorting process compares and arranges all user entries once per query.
Sorting takes more time as the number of users increases.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 30 comparisons |
| 100 | About 700 comparisons |
| 1000 | About 10,000 comparisons |
Pattern observation: The number of operations grows faster than the number of users, roughly multiplying by a bit more than n each time.
Time Complexity: O(n log n)
This means sorting takes a bit more than linear time, growing faster as the list gets bigger but still efficient for many users.
[X] Wrong: "Sorting takes the same time no matter how many users there are."
[OK] Correct: Sorting compares many pairs of items, so more users mean more comparisons and longer time.
Understanding how sorting affects query time helps you explain data fetching performance clearly and confidently.
"What if we added a filter to reduce the number of users before sorting? How would the time complexity change?"
Practice
orderBy argument do in a GraphQL query?Solution
Step 1: Understand the purpose of
TheorderByorderByargument is used to arrange data in a specific order based on a field.Step 2: Identify sorting behavior
It sorts data either ascending (ASC) or descending (DESC) by the chosen field.Final Answer:
It sorts the returned data by a specified field and direction. -> Option AQuick Check:
Sorting = orderBy usage [OK]
- Confusing orderBy with filtering or limiting results
- Thinking orderBy groups data instead of sorting
- Assuming orderBy changes the data fields returned
name in ascending order using orderBy?Solution
Step 1: Review GraphQL orderBy syntax
The correct syntax uses an object with the field as key and direction as value inside curly braces.Step 2: Match syntax to options
{ users(orderBy: { name: ASC }) { id name } } matches this pattern:{ name: ASC }. Others use incorrect formats.Final Answer:
{ users(orderBy: { name: ASC }) { id name } } -> Option CQuick Check:
Correct orderBy syntax = { users(orderBy: { name: ASC }) { id name } } [OK]
- Using parentheses or brackets incorrectly in orderBy
- Placing direction before field name
- Omitting curly braces around orderBy argument
{ products(orderBy: { price: DESC }) { id name price } }What will be the order of products in the result?
Solution
Step 1: Identify sorting field and direction
The query usesorderBy: { price: DESC }, meaning sort by price descending.Step 2: Understand descending order
Descending order means highest values come first, then lower values.Final Answer:
Products sorted by price from highest to lowest. -> Option AQuick Check:
DESC means highest to lowest [OK]
- Confusing DESC with ascending order
- Assuming sorting is by name instead of price
- Thinking orderBy does not affect order
{ posts(orderBy: { date: ASC }) { id title date } }But you get an error. What is the likely mistake?
Solution
Step 1: Check schema fields
Ifdateis not defined in the posts schema, sorting by it causes an error.Step 2: Validate other options
orderBy accepts an object, ASC is valid, and sortBy is not standard GraphQL.Final Answer:
Thedatefield is missing from the schema. -> Option DQuick Check:
Sorting field must exist in schema [OK]
- Using invalid direction keywords
- Confusing orderBy with other arguments
- Assuming orderBy requires a list always
department ascending, then by salary descending. Which orderBy argument correctly achieves this?Solution
Step 1: Understand multi-level sorting syntax
To sort by multiple fields,orderByusually takes a list of objects, each specifying a field and direction.Step 2: Match correct syntax
{ employees(orderBy: [{ department: ASC }, { salary: DESC }]) { id name department salary } } uses a list with two objects: first{ department: ASC }, then{ salary: DESC }, which is correct.Step 3: Check other options
{ employees(orderBy: { department: ASC, salary: DESC }) { id name department salary } } uses a single object with two keys, which may not guarantee order. { employees(orderBy: [department: ASC, salary: DESC]) { id name department salary } } has invalid syntax. { employees(orderBy: { salary: DESC, department: ASC }) { id name department salary } } reverses sorting priority.Final Answer:
{ employees(orderBy: [{ department: ASC }, { salary: DESC }]) { id name department salary } } -> Option BQuick Check:
Multi-field orderBy = list of objects [OK]
- Using a single object for multiple sort fields
- Reversing the order of sorting priorities
- Incorrect syntax for list of sorting fields
Ready to prove it?
Pass the test and this pattern moves unknown
