HTML View: Sort by parameter passed through URL.

HTML View: Sort by parameter passed through URL.

I have an HTML view in which there's a for each record in form loop.
Now I'd like to sort these results by a parameter passed through the URL.

For example, if ?sort=name is passed on with the URL, I'd like to execute the loop  for each record in form  sort by name.
Is there any way to implement this directly instead of performing consecutive if - else loops containing the same piece of code, except for the sort by parameter?

E.g. for now this is implemented like this :
  1. if ( input.sort == "name" ) {
  2.       for each R in F sort by name {
  3.             piece of code A
  4.       }
  5. } else if ( input.sort == "date" ) {
  6.       for each R in F sort by date {
  7.             piece of code A
  8.       }
  9. } ...
Thanks in advance !