0

I have a program in C# that needs to sort 12 names into 3 groups of 4, six times, and dump them into a CSV file. It uses a textbox for user input to be put into an array and displayed in a datagrid. Then the program reads the names and puts them into a CSV file and also displays them into another datagrid. The code below only does the 3 groups of 4 once, and they are in the order that the user enters. How do I get 5 more groups of random (or at least somewhat random) names in 3 groups of 4?

public void des()
    {

        for (int x = 0; x < 7; x++)
        {
            //for (int o = 0; o < desArayPlayers.Length; o++)
            {
                int playersCount = 0;
                int k = 0;
                for (int i = 1; i < 4; i++)
                {
                    switch (i)
                    {
                        case 1:
                            k = 0;
                            break;
                        case 2:
                            k = 4;
                            break;
                        case 3:
                            k = 8;
                            break;
                        default:
                            break;
                    }

                    for (int j = k; j < k + 4; j++)
                    {
                        sortingGroupsArayPlayers[playersCount] = "Group " + i.ToString() + "  " + arayPlayers[j];
                        //MessageBox.Show(desArayPlayers[playersCount]);
                        playersCount++;

                    }
                }
            }



        }

    }
Liam G
  • 771
  • 2
  • 9
  • 26
  • sort or randomly divide them up?? – BugFinder Jul 25 '16 at 12:10
  • See http://stackoverflow.com/questions/1287567/is-using-random-and-orderby-a-good-shuffle-algorithm to learn how to generate random secuence. And then just divide into 3*4 groups. – Leonid Malyshev Jul 25 '16 at 12:19
  • `for (int x = 0; x < 7; x++)` that will loop 7 times instead of 6 – Jeroen van Langen Jul 25 '16 at 12:26
  • How do you want to store the groups - as an array/list of array/list of array/list of 4 names, or as an array/list of array/list of 4 names? (Hmm, that sounds a bit confusing... perhaps a higher-level class wrapper would be better) – Matthew Watson Jul 25 '16 at 12:40

2 Answers2

2

This will randomly order your items, group them in groups of 3 and then return a collection of arrays of those items within the groups.

All this happens in the for loop so you can do with it what you need:)

List<List<int[]>> iterationResults = new List<List<int[]>>();
for (int iteration = 0; iteration < 6; iteration++)
{
    iterationResults.Add(collection.OrderBy(item => Guid.NewGuid())
                .Select((item, index) => new { Item = item, GroupKey = index % 4 })
                .GroupBy(item => item.GroupKey)
                .Select(group => group.Select(item => item.Item).ToArray()).ToList());
}
Gilad Green
  • 36,708
  • 7
  • 61
  • 95
0
string[] names = new string[] { "john", "bob", "tim","dickson","bobby","tomson","richard","james","bond","rick","simon","ray" };
Random rnd = new Random();
string[] MyRandomArray = names.OrderBy(x => rnd.Next()).ToArray();
List<string[]> Groups = new List<string[]>();
for (int i = 0; i < 3; i++)//3 groups
   Groups.Add(MyRandomArray.Skip((i)*4).Take(4).Select(x => x.ToString()).ToArray());
Neil
  • 641
  • 1
  • 7
  • 21