- Home
- API Design
- How to Filter, Sort & Sea ...

Filtering, sorting and searching are basic features that an API must possess to serve data to the client application efficiently.
Sorting allows you to order the results by any field, in ascending or descending order. Ascending and descending mean for alphabetical, numerical, and date-based responses. Sorting is an important feature for any API endpoint that returns a lot of data.
| Type | Example |
| Alphabetical | A – Z |
| Numerical | 1 – 9 |
| Date | 01-01-2022 – Today |
| Type | Example |
| Alphabetical | Z – A |
| Numerical | 9 – 1 |
| Date | Today – 01-01-2022 |
If you only need to sort one column at a time, you could put the column name in sort_by and the sort direction in order.
SELECT * FROM users ORDER BY first_name ASCSELECT * FROM users ORDER BY first_name DESCIf the ability to sort multiple columns is required, you could comma-separate each column:order pair and put it in one sort parameter. This could also be used for a single column if you prefer the syntax.
SELECT * FROM users
ORDER BY first_name ASC, age DESCIn filtering URL parameters is the easiest way to add basic filtering to REST APIs. There are several ways to handle it. Some APIs will use a POST and pass all the data in the body of the request for searching. But a GET is preferable.
Search by a single column.
SELECT * FROM users
WHERE first_name = 'Sam'Multiple options for a single column can be handled in different ways.
SELECT * FROM users
WHERE first_name IN ('Tina', 'Sam')Exact number search on a column.
SELECT * FROM users
WHERE age = 31use gt: to handle greater than and gte: greater than or equal
SELECT * FROM users
WHERE age > 31use lt: to handle less than and lte: less than or equal
SELECT * FROM users
WHERE age < 31Range between two number values, using [and] in between them could be one option. Depending on options like greater than and greater than or equal, or other options.
SELECT * FROM users
WHERE age > 22 AND age < 30If you need a range between two dates, you can use start and end, or since and to.
SELECT * FROM users
WHERE created_at BETWEEN '01-01-2000' AND '09-09-2022'When full text search is used as a mechanism of retrieving resource instances for a specific type of resource, it can be exposed on the API as a query parameter on the resource’s endpoint. Here, ?q represents the query. Search queries should be passed straight to the search engine and API output should be in the same format as a normal list result.
A string that contains the text for the search query. The Search REST API service supports both HTTP POST and HTTP GET requests.
| GET requests | POST requests |
| https://www.api.com/_api/search/query?querytext=apitier | https://www.api.com/_api/search/postquery |