Thinking of adding a `sort` parameter to these functions to make it more obvious to what's happening under the hood. One issue is that when sorting, the order of the groups are changing. So maybe make this optional.