I want to allow users to choose which property they would like to sort by then choose whether to order by ascending or descending.
For example, to sort based on the title and by descending order you would make a call to the api like so:
api/blogs?sortBy=title&orderBy=desc
Is this the best way to go about doing this?
I can't find anything useful online and most posts explain that sort by and order by are basically the same thing.
What are peoples opinions on this?
There is not the "best" way to do that. For any API, basically it's more important to have consistency, follow the same strategy across the endpoints and be flexible enough to future improvements.
So, we have some options. For example, if you need to order by title with DESC and by author with ASC, you can choose:
/api/blogs?sort=-title,+author
/api/blogs?sort=desc(title),asc(author)
/api/blogs?sort=title:desc,author:asc
Some people use the format used in your example. But it's have the limitation to not be flexible enough to allow the sort by multiple fields, because sort and order are separated.
This is a very good way of doing it.
Look at smaller retail websites, they often use Get, so people can bookmark their search and sorting.
If you want multi-column sorting, just use a separator-character.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With