Given
IEnumerable<T> first;
IEnumerable<T> second;
and that both first
and second
are ordered by a comparer Func<T, T, int>
that returns 0 for equality, -1 when the first is "smaller" and 1 when the second is "smaller".
Is there a straight-forward way using LINQ to merge the two sequences in a way that makes the resulting sequence also ordered by the same comparer?
We're currently using a hand-crafted algorithm that works, but the readability of a straight-forward LINQ statement would be preferable.
You could define an extension method for this. Something like
public static IEnumerable<T> MergeSorted<T>(this IEnumerable<T> first, IEnumerable<T> second, Func<T, T, int> comparer)
{
using (var firstEnumerator = first.GetEnumerator())
using (var secondEnumerator = second.GetEnumerator())
{
var elementsLeftInFirst = firstEnumerator.MoveNext();
var elementsLeftInSecond = secondEnumerator.MoveNext();
while (elementsLeftInFirst || elementsLeftInSecond)
{
if (!elementsLeftInFirst)
{
do
{
yield return secondEnumerator.Current;
} while (secondEnumerator.MoveNext());
yield break;
}
if (!elementsLeftInSecond)
{
do
{
yield return firstEnumerator.Current;
} while (firstEnumerator.MoveNext());
yield break;
}
if (comparer(firstEnumerator.Current, secondEnumerator.Current) < 0)
{
yield return firstEnumerator.Current;
elementsLeftInFirst = firstEnumerator.MoveNext();
}
else
{
yield return secondEnumerator.Current;
elementsLeftInSecond = secondEnumerator.MoveNext();
}
}
}
}
Usage:
var s1 = new[] { 1, 3, 5, 7, 9 };
var s2 = new[] { 2, 4, 6, 6, 6, 8 };
var merged = s1.MergeSorted(s2, (a, b) => a > b ? 1 : -1).ToList();
Console.WriteLine(string.Join(", ", merged));
Output:
1, 2, 3, 4, 5, 6, 6, 6, 7, 8, 9
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With