SortedMergeExtensionSortedMergeTSource(IEnumerableTSource, OrderByDirection, IEnumerableTSource) Method

Merges two or more sequences that are in a common order (either ascending or descending) into a single sequence that preserves that order.

Definition

Namespace: MoreLinq.Extensions
Assembly: MoreLinq (in MoreLinq.dll) Version: 3.4.0+b99a6a8cc504caf2d48372fe54a2f8116c59cd0c
C#
public static IEnumerable<TSource> SortedMerge<TSource>(
	this IEnumerable<TSource> source,
	OrderByDirection direction,
	params IEnumerable<TSource>[] otherSequences
)

Parameters

source  IEnumerableTSource
The primary sequence with which to merge.
direction  OrderByDirection
The ordering that all sequences must already exhibit.
otherSequences  IEnumerableTSource
A variable argument array of zero or more other sequences to merge with.

Type Parameters

TSource
The type of the elements of the sequence.

Return Value

IEnumerableTSource
A merged, order-preserving sequence containing all of the elements of the original sequences.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type IEnumerableTSource. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).

Remarks

Using SortedMergeTSource(IEnumerableTSource, OrderByDirection, IEnumerableTSource) on sequences that are not ordered or are not in the same order produces undefined results.

SortedMergeTSource(IEnumerableTSource, OrderByDirection, IEnumerableTSource) uses performs the merge in a deferred, streaming manner.

Here is an example of a merge, as well as the produced result:

C#
var s1 = new[] { 3, 7, 11 };
var s2 = new[] { 2, 4, 20 };
var s3 = new[] { 17, 19, 25 };
var merged = s1.SortedMerge(OrderByDirection.Ascending, s2, s3);
var result = merged.ToArray();
// result will be:
// { 2, 3, 4, 7, 11, 17, 19, 20, 25 }

See Also