bring in sorting changes from client.js

Brings in the fixes to the sorting that were done in client.js