Refactor sorting of results into a single function

>This PR contains all the changes of the `auth` branch.

## Changes in this PR:
* Create a single function to take care of sorting results from queries
* Make sorting function accept the sorting method as a parameter
* Sorting method can be 'single', 'multiple' or 'teams', depending on the type of results being sorted

@robertkluin-wf @beaulyddon-wf @amandeepgill-wf