fix the sorting and pagination bug