Fix incorrect variable order and names
Add tests to check if query results are sorted according to the index definition when no explicit sort is specified on query