Method SortedMerge
| Edit this page View SourceSortedMerge<TSource>(IEnumerable<TSource>, params IEnumerable<TSource>[])
Merges two or more sequences that are in a common order into a single sequence that preserves that order.
Declaration
public static IEnumerable<TSource> SortedMerge<TSource>(this IEnumerable<TSource> source, params IEnumerable<TSource>[] otherSequences)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | The primary sequence with which to merge |
IEnumerable<TSource>[] | otherSequences | A variable argument array of zero or more other sequences to merge with |
Returns
Type | Description |
---|---|
IEnumerable<TSource> | A merged, order-preserving sequence containing all of the elements of the original sequences |
Type Parameters
Name | Description |
---|---|
TSource | The type of the elements of the sequence |
Remarks
Using SortedMerge on sequences that are not ordered or are not in the same order produces undefined results.
This method uses deferred execution and streams its results.
Examples
The following code example demonstrates how to merge two or more sequences that are in a common order into a single sequence that preserves that order using SortedMerge
.
var s1 = new[] { 3, 7, 11 };
var s2 = new[] { 2, 4, 20 };
var s3 = new[] { 17, 19, 25 };
// Execute a sorted merge of multiple sequences into a single sequence
var result = s1
.SortedMerge(s2, s3);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// One possible random output is as follows:
// [2, 3, 4, 7, 11, 17, 19, 20, 25]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
SortedMerge<TSource>(IEnumerable<TSource>, IComparer<TSource>?, params IEnumerable<TSource>[])
Merges two or more sequences that are in a common order into a single sequence that preserves that order.
Declaration
public static IEnumerable<TSource> SortedMerge<TSource>(this IEnumerable<TSource> source, IComparer<TSource>? comparer, params IEnumerable<TSource>[] otherSequences)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | The primary sequence with which to merge |
IComparer<TSource> | comparer | An IComparer<T> to compare elements |
IEnumerable<TSource>[] | otherSequences | A variable argument array of zero or more other sequences to merge with |
Returns
Type | Description |
---|---|
IEnumerable<TSource> | A merged, order-preserving sequence containing all of the elements of the original sequences |
Type Parameters
Name | Description |
---|---|
TSource | The type of the elements of the sequence |
Remarks
Using SortedMerge on sequences that are not ordered or are not in the same order produces undefined results.
This method uses deferred execution and streams its results.
Examples
The following code example demonstrates how to merge two or more sequences that are in a common order into a single sequence that preserves that order using SortedMerge
.
var s1 = new[] { 3, 7, 11 };
var s2 = new[] { 2, 4, 20 };
var s3 = new[] { 17, 19, 25 };
// Execute a sorted merge of multiple sequences into a single sequence
var result = s1
.SortedMerge(Comparer<int>.Default, s2, s3);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// One possible random output is as follows:
// [2, 3, 4, 7, 11, 17, 19, 20, 25]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
SortedMerge<TSource>(IEnumerable<TSource>, OrderByDirection, params IEnumerable<TSource>[])
Merges two or more sequences that are in a common order (either ascending or descending) into a single sequence that preserves that order.
Declaration
public static IEnumerable<TSource> SortedMerge<TSource>(this IEnumerable<TSource> source, OrderByDirection direction, params IEnumerable<TSource>[] otherSequences)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | The primary sequence with which to merge |
OrderByDirection | direction | A direction in which to order the elements (ascending, descending) |
IEnumerable<TSource>[] | otherSequences | A variable argument array of zero or more other sequences to merge with |
Returns
Type | Description |
---|---|
IEnumerable<TSource> | A merged, order-preserving sequence containing all of the elements of the original sequences |
Type Parameters
Name | Description |
---|---|
TSource | The type of the elements of the sequence |
Remarks
Using SortedMerge
on sequences that are not ordered or are not in the same order produces undefined results.
This method uses deferred execution and streams its results.
Examples
The following code example demonstrates how to merge two or more sequences that are in a common order into a single sequence that preserves that order using SortedMerge
.
var s1 = new[] { 11, 7, 3 };
var s2 = new[] { 20, 4, 2 };
var s3 = new[] { 25, 19, 17 };
// Execute a sorted merge of multiple sequences into a single sequence
var result = s1
.SortedMerge(OrderByDirection.Descending, s2, s3);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// One possible random output is as follows:
// [25, 20, 19, 17, 11, 7, 4, 3, 2]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
SortedMerge<TSource>(IEnumerable<TSource>, OrderByDirection, IComparer<TSource>?, params IEnumerable<TSource>[])
Merges two or more sequences that are in a common order (either ascending or descending) into a single sequence that preserves that order.
Declaration
public static IEnumerable<TSource> SortedMerge<TSource>(this IEnumerable<TSource> source, OrderByDirection direction, IComparer<TSource>? comparer, params IEnumerable<TSource>[] otherSequences)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | The primary sequence with which to merge |
OrderByDirection | direction | A direction in which to order the elements (ascending, descending) |
IComparer<TSource> | comparer | An IComparer<T> to compare elements |
IEnumerable<TSource>[] | otherSequences | A variable argument array of zero or more other sequences to merge with |
Returns
Type | Description |
---|---|
IEnumerable<TSource> | A merged, order-preserving sequence containing all of the elements of the original sequences |
Type Parameters
Name | Description |
---|---|
TSource | The type of the elements of the sequence |
Remarks
Using SortedMerge
on sequences that are not ordered or are not in the same order produces undefined results.
This method uses deferred execution and streams its results.
Examples
The following code example demonstrates how to merge two or more sequences that are in a common order into a single sequence that preserves that order using SortedMerge
.
var s1 = new[] { 11, 7, 3 };
var s2 = new[] { 20, 4, 2 };
var s3 = new[] { 25, 19, 17 };
// Execute a sorted merge of multiple sequences into a single sequence
var result = s1
.SortedMerge(
OrderByDirection.Descending,
Comparer<int>.Default,
s2, s3);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// One possible random output is as follows:
// [25, 20, 19, 17, 11, 7, 4, 3, 2]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|