Corrected sorting for groups.