Fix title on sort

This pull request fix #122 and refactor `Dataset.sort`.

The `sort` code had a lot of common code in both if/else blocks, so I changed it to share this code.