32

I have a RESTful web application that supports multiple sort fields on a collection of items. Is there a common convention for encoding these sort fields into the query string of a URL? I'm considering a pattern like the following:

http://myapp.com/books?sort=author:asc,datepublished:desc&count=12 

This would sort the collection of books by author and then by date published.

Basically, I need a convenient way for the name-value pairs in my query string to have name-value pairs of their own. I'll need to do something similar to the above example for filter parameters, too.

Does Rails or ASP.NET MVC have a pattern for this? Are there other frameworks that have established ways for dealing with this issue? I'd rather use a familiar format than roll my own.

I'd also prefer a format that uses as little URL percent-encoding as possible.

4
  • Good question -- I've wondered the same thing myself. Commented Feb 23, 2009 at 16:56
  • It looks like you're making a JSON-ish format; which I would avoid because someone may interpret it -as- JSON, in which case the notion of ordering is lost. Commented Feb 23, 2009 at 16:58
  • Good point, Tom. That's why I'm asking for feedback! Commented Feb 23, 2009 at 18:37
  • I've updated my sample pattern to remove the braces, per Tom's feedback. They weren't necessary anyway. Commented Feb 23, 2009 at 19:17

3 Answers 3

39
+100

I've done this before using the standard SQL sorting syntax. There are numerous parsing functions and techniques out there.

http://myapp.com/books?sort=author asc,datepublished desc&count=12

which would be encoded to

http://myapp.com/books?sort=author+asc,datepublished+desc&count=12
Sign up to request clarification or add additional context in comments.

5 Comments

This is a good way to go as it is also compatible with Solr, a widely-used open-source search engine.
+1, Plus Comma list is probably better than Comma Comma list.
Simple. I like it. We have a winner!
If you use those values directly you'll have a pretty security hole for SQL-injection. I'm warning you ;)
@BYK, good point, be sure to scrub your results before passing them to SQL, if your planning that.
22

For REST, I prefer a more intuitive syntax:

http://myapp.com/books?sort=author,-datepublished&count=12

Easy to remember... (means: ORDER BY author ASC, datepublished DESC)

note: removed "+" prefix because is a reserved word

3 Comments

what does + and - stand for ? i mean what is asc and what i desc.
+ is a special character in URL and will be encoded. I wonder how you tackled that
@pm1090: I edited to remove "+" plus sign, as you say is special character...
1

What about a fully PHP compliant version like this one:

http://myapp.com/books?sort[0][name]=author&sort[0][dir]=asc&sort[1][name]=datepublished&sort[1][dir]=desc&count=12

A bit longer but much convinient and as I've said, compliant with PHP. ASP.NET might have implemented support for this format also.

This will create an array, sort, directly where each element of sort has a name and dir property.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.