Sorting
PhantomJSON supports sorting by one or more fields.
Syntax
Section titled “Syntax”?sort=field:direction| Parameter | Values | Default |
|---|---|---|
field |
Any sortable field | id |
direction |
asc or desc |
asc |
Single Field Sort
Section titled “Single Field Sort”curl "https://api.phantomjson.app/v1/users?sort=firstName:asc"Multiple Field Sort
Section titled “Multiple Field Sort”Separate multiple sort fields with commas:
curl "https://api.phantomjson.app/v1/users?sort=lastName:asc,firstName:desc"Sortable Fields
Section titled “Sortable Fields”| Resource | Sortable Fields |
|---|---|
| Users | id, firstName, lastName, email |
| Posts | id, userId, title |
| Comments | id, postId, name |
| Todos | id, userId, title |
| Albums | id, userId, title |
| Photos | id, albumId, title |
| Products | id, name, price |
Examples
Section titled “Examples”Sort by Name
Section titled “Sort by Name”curl "https://api.phantomjson.app/v1/users?sort=firstName:asc"Sort by Date (Newest First)
Section titled “Sort by Date (Newest First)”curl "https://api.phantomjson.app/v1/posts?sort=createdAt:desc"Sort by Price
Section titled “Sort by Price”curl "https://api.phantomjson.app/v1/products?sort=price:asc"Multiple Sort Fields
Section titled “Multiple Sort Fields”# Sort by last name, then first namecurl "https://api.phantomjson.app/v1/users?sort=lastName:asc,firstName:asc"Combining with Other Parameters
Section titled “Combining with Other Parameters”Sorting works with pagination, filtering, and search:
# Sorted, paginated resultscurl "https://api.phantomjson.app/v1/users?sort=firstName:asc&page=1&limit=10"
# Sorted, filtered resultscurl "https://api.phantomjson.app/v1/products?category=smartphones&sort=price:desc"