Fix bugs in grouping and sorting functions with distances
Merge pull request #416 from emmanuel-keller/1.5

Fix bugs in grouping and sorting functions with distances