5

Not sure if there's a algorithm to describe this problem but are there any elegant methods to combine the list in a custom sequence. For example:

List<string> list1 = new List<string>();
List<string> list2 = new List<string>();
List<string> list3 = new List<string>();

list1.Add("a");
list1.Add("b");
list1.Add("c");

list2.Add("d");
list2.Add("e");
list2.Add("f");

list3.Add("g");
list3.Add("h");
list3.Add("i");

List<string> combined = new List<string>();

I would like the contents of combined to contain a sequence as follows:

a //First record in list1
d //First record in list2
g //First record in list3
b //Second record in list1
e //Second record in list2
h //Second record in list3
c //Third record in list1 
f //Third record in list2 
i //Third record in list3 

The number of records in each list may not be equal.

EDIT

When the number of records in each list may not be equal i mean:

List<string> list1 = new List<string>();
List<string> list2 = new List<string>();
List<string> list3 = new List<string>();

list1.Add("a");

list2.Add("b");
list2.Add("c");

list3.Add("d");
list3.Add("e");
list3.Add("f");

List<string> combined = new List<string>();

Expected results:

a //First record in list1
b //First record in list2
d //First record in list3
c //Second record in list2
e //Second record in list3
f //Third record in list3
atp03
  • 3,539
  • 3
  • 17
  • 20

6 Answers6

4

Not sure if there's a name. Merging? Splicing? But the code is easy.

var lists = new [] { list1, list2, list3 };
var combined = new List<string>(lists.Sum(l => l.Count));    

for (var i = 0; i < lists.Max(l => l.Count); i++)
{
   foreach (var list in lists)
   { 
      if (i < list.Count)
          combined.Add (list[i])
   }
}
Tim Rogers
  • 21,297
  • 6
  • 52
  • 68
2
int MaxCount = List1.Count; //Or whatever the highest list count.

            for (int i = 0; i < MaxCount; i++)
                {
                    if( list1.Count > i)
                    combined.Add(list1[i]);

                    if( list2.Count > i)
                    combined.Add(list2[i]);

                    if( list3.Count > i)
                    combined.Add(list3[i]);
                }
Muhammad Hani
  • 8,476
  • 5
  • 29
  • 44
1

You could use this generic Merge<T> function:

    public static IEnumerable<T> Merge<T>(params List<T>[] lists)
    {
        var max = lists.Max(list => list.Count());

        for (int i = 0; i < max; i++)
        {
            foreach (var list in lists)
            {
                if (i < list.Count)
                {
                    yield return list[i];
                }
            }
        }
    }

Example usage:

var merged = Merge(list1, list2, list3).ToList();
Oliver
  • 8,794
  • 2
  • 40
  • 60
1

Unfortunately there are no built in function for it. You should loop each list and add it to a new custom list.

Example:

List<string> result = new List<string>();
int listLength = Math.Max(list1.Count, Math.Max(list2.Count, list3.Count)); // get the largest list length
for(int index = 0; index < listLength; i++){
  if(list1.Count > index) result.Add(list1[index);
  if(list2.Count > index) result.Add(list3[index);
  if(list3.Count > index) result.Add(list3[index);
}
Fendy
  • 4,565
  • 1
  • 20
  • 25
0

A quick LINQPad example:

void Main()
{
    List<string> list1 = new List<string>();
    List<string> list2 = new List<string>();
    List<string> list3 = new List<string>();

    list1.Add("a");
    list1.Add("b");
    list1.Add("c");

    list2.Add("d");
    list2.Add("e");
    list2.Add("f");

    list3.Add("g");
    list3.Add("h");
    list3.Add("i");

    Merge(new[] { list1, list2, list3}, (c1, c2) => c1 + c2).SelectMany(s => s).Dump();
}

IEnumerable<T> Merge<T>(IEnumerable<IEnumerable<T>> sources, Func<T, T, T> combine)
{
    return sources.Aggregate((s1, s2) => s1.Zip(s2, combine));
}

The result is an IEnumerable<char> but this is simple enough to convert into a List<string> if necessary.

Alex
  • 7,639
  • 3
  • 45
  • 58
-2

You can either use Concate() or AddRange, see this thread for more info about the difference between these methods

Community
  • 1
  • 1