Making sorting work with pagination