Hi,
I am writing a Windows Form program, and currently it has 3 Lists. I need to loop through all three, and use each possible combination, so am using three nested for loops.
I found that if the outer loop had less elements than one of the inner loops/lists, some were missed off, as I need to save all the possible combinations of list a + b + c.
This is what I wrote:
My problem is I would like to be able to have 4 (or more if possible) lists, and to calculate what order to process the lists will be exponentially more complex.Code:public int CalculateOrderToProcessLists()//for three lists { int order = 0; if (sListOne.Count() > sListTwo.Count() && sListTwo.Count() > sListThree.Count()) { order = 123; } else if(sListOne.Count() > sListTwo.Count() && sListTwo.Count() < sListThree.Count() && sListOne.Count() > sListThree.Count()) { order = 132; } else if (sListOne.Count() < sListTwo.Count() && sListTwo.Count() > sListThree.Count() && sListOne.Count() > sListThree.Count()) { order = 213; } else if (sListOne.Count() < sListTwo.Count() && sListTwo.Count() > sListThree.Count() && sListOne.Count() < sListThree.Count()) { order = 231; } else if (sListOne.Count() > sListTwo.Count() && sListTwo.Count() < sListThree.Count() && sListOne.Count() < sListThree.Count()) { order = 312; } else if (sListOne.Count() < sListTwo.Count() && sListTwo.Count() < sListThree.Count()) { order = 321; } else { order = 999; } return order; }
I was wondering if anyone could help me come up with a better way of comparing the sizes of the lists, so that I can use the nested loops of say 4 lists, but being able to use N number of lists would be awesome.
Thanks for any help, it's most appreciated.