Check that we have a sort before iterating over the sort property

The fetch breaks when working with memory data since the sort property of the query is undefined.