Fixed bug in clustering and added test.
Merge pull request #314 from SimTk/sorted

Fixed bug in clustering and added test.