fix the variable group sort method

The sorting function I wrote was too lazy!