Fix bug when sorting data subsets