Fix a bug with sorting