Hello!

I am creating a program for teachers to use in their classrooms. Every classroom have a list of users (students) and each student has a field called “category” and it can be one of 4 options.

I want to sort the classrooms users into groups based on their “category”. The size of the groups can vary depending on the teacher’s preference.

The different categories that the users can have are red, yellow, green and blue.

If the groups consist of 2 people, there are certain restrictions (this condition is the best case scenario, if users of that certain type do not exist, groups that defy this condition can still be created):

*red can’t be with green*

*yellow can’t be with blue*

if the groups are anything else they should optimally *contain one of each category*

Example 1 (4 people)

1 red

1 yellow

1 green

1 blue

Example 2 (7 people)

2 red

2 yellow

2 green

1 blue

Example 3 (3 people)

1 red

1 yellow

1 green

I would then like to present these groups.

The number of students can vary from classroom to classroom.

How would I go about creating this function?

Thanks for help!