Fix sort when query is empty string

When the query is the empty string, the sorting function doesn't perform correctly.

This PR contains a failing test, and a fix to make sorting work correctly under this condition.