Use mergesort in groups sorting to maintain original order

Running table tests failed on mac because the quicksort alg gives a different order than on linux.  There is no guarantee that quicksort is stable, so this PR switches the sort algorithm to mergesort, which *is* stable.