Fix sorting bugs in IndexCollection and the mock database