refactor groupSameRanks