Split up sort SQL to ensure the query is valid

Sorting can add both a JOIN and a GROUP/ORDER to the query.
The WHERE for my search upgrades needs to come between those 2.
Merge pull request #3 from ARISGames/sort-fix

Split up sort SQL to ensure the query is valid