Alternating items in a collection Is there any better way how to mix items in list? In my example: I want to sort list items as male, female, male, ... My solution is probably very time and resource demanding. Can I do it somehow with LINQ?

private List&lt;Child&gt; MixGender(List&lt;Child&gt; children)
    {
        List&lt;Child&gt; male = new List&lt;Child&gt;() { };
        List&lt;Child&gt; female = new List&lt;Child&gt;() { };

        male = children.Where(c =&gt; c.Sex == "male").ToList();
        female = children.Where(c =&gt; c.Sex == "female").ToList();

        int childrenCount = children.Count;
        int indexMale = 0;
        int indexFemale = 0;

        children.Clear();

        for (int i = 0; i &lt; childrenCount; i++)
        {
            if (i % 2 == 1)
            {
                if (indexMale &lt; male.Count)
                {
                    children.Add(male[indexMale]);
                    indexMale++;
                }
                else
                {
                    children.Add(female[indexFemale]);
                    indexFemale++;
                }
            }
            else
            {
                if (indexFemale &lt; female.Count)
                {
                    children.Add(female[indexFemale]);
                    indexFemale++;
                }
                else
                {
                    children.Add(male[indexMale]);
                    indexMale++;
                }
            }
        }

        return children;
    }
Not a fan of LINQ, yet? Then look here... You could start by grouping Child by Sex into a new lookup, this would replace the two Wheres. In the next step you Zip them together and take one item from each collection at a time. You create from then new tiny collections that you finally flatten with SelectMany. No calculation necessary.

var groups = items.ToLookup(i =&gt; i.Sex);
var alternating = 
    groups["male"]
        .Zip(groups["female"], (x, y) =&gt; new[] { x, y })
        .SelectMany(t =&gt; t)
        .ToList();


This will however require that both collections have the same length because Zip will go only as far as the shorter collection.



Some nice improvement to you method would be to make it work for any group of items. When you pass it a custom comparer you can GroupBy the items and iterate over each group until there is nothing left to yield. Since enumerators are disposable you need to make sure to dispose them properly at the end thus the finally block.

public static IEnumerable&lt;T&gt; Interleave&lt;T&gt;(this IEnumerable&lt;T&gt; source, IEqualityComparer&lt;T&gt; comparer)
{
    var groups = source.GroupBy(x =&gt; x, comparer);
    var enumerators = groups.Select(g =&gt; g.GetEnumerator()).ToList();
    try
    {
        var any = false;
        do
        {
            any = false;
            foreach (var e in enumerators)
            {
                if (e.MoveNext())
                {
                    any = true;
                    yield return e.Current;
                }
            }
        } while (any);
    }
    finally
    {
        foreach (var e in enumerators)
        {
            e.Dispose();
        }
    }
}


An example usage could then look like this:

var innerComparer = StringComparer.OrdinalIgnoreCase;
var comparer = EqualityComparerFactory&lt;Child&gt;.Create
(
    (x, y) =&gt; innerComparer.Equals(x.Sex, y.Sex), 
    obj =&gt; innerComparer.GetHashCode(obj.Sex)
);
var result = items.Interleave(comparer);