Method DensePartialSortBy
| Edit this page View SourceDensePartialSortBy<TSource, TKey>(IEnumerable<TSource>, int, Func<TSource, TKey>)
Executes a partial sort of the top count
elements of a sequence, including ties,
according to the key for each element. If count
is less than the total number of elements
in source
, then this method will improve performance.
Declaration
public static IEnumerable<TSource> DensePartialSortBy<TSource, TKey>(this IEnumerable<TSource> source, int count, Func<TSource, TKey> keySelector)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | The source sequence. |
int | count | Number of (maximum) elements to return. |
Func<TSource, TKey> | keySelector | A function to extract a key from an element. |
Returns
Type | Description |
---|---|
IEnumerable<TSource> | A sequence containing at most top |
Type Parameters
Name | Description |
---|---|
TSource | Type of elements in the sequence. |
TKey | Type of keys. |
Remarks
This is an O(n * log(K))
operation where K
is count
.
This method is implemented by using deferred execution. However, source
will be consumed
in it's entirety immediately when first element of the returned sequence is consumed.
This method performs a stable sort; that is, if the keys of two elements are equal, the order of the elements is preserved. In contrast, an unstable sort does not preserve the order of elements that have the same key.
Examples
The following code example demonstrates how to get the top N items of a sequence using DensePartialSort
.
var sequence = new[]
{
(key: 5, text: "1"),
(key: 5, text: "2"),
(key: 4, text: "3"),
(key: 4, text: "4"),
(key: 3, text: "5"),
(key: 3, text: "6"),
(key: 2, text: "7"),
(key: 2, text: "8"),
(key: 1, text: "9"),
(key: 1, text: "10"),
};
// Get the top N sets of items
var result = sequence
.DensePartialSortBy(
3,
x => x.key);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [(1, 9), (1, 10), (2, 7), (2, 8), (3, 5), (3, 6)]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentOutOfRangeException |
|
DensePartialSortBy<TSource, TKey>(IEnumerable<TSource>, int, Func<TSource, TKey>, OrderByDirection)
Executes a direction
partial sort of the top count
elements of a
sequence, including ties, according to the key for each element. If count
is less than
the total number of elements in source
, then this method will improve performance.
Declaration
public static IEnumerable<TSource> DensePartialSortBy<TSource, TKey>(this IEnumerable<TSource> source, int count, Func<TSource, TKey> keySelector, OrderByDirection direction)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | The source sequence. |
int | count | Number of (maximum) elements to return. |
Func<TSource, TKey> | keySelector | A function to extract a key from an element. |
OrderByDirection | direction | The direction in which to sort the elements |
Returns
Type | Description |
---|---|
IEnumerable<TSource> | A sequence containing at most top |
Type Parameters
Name | Description |
---|---|
TSource | Type of elements in the sequence. |
TKey | Type of keys. |
Remarks
This is an O(n * log(K))
operation where K
is count
.
This method is implemented by using deferred execution. However, source
will be consumed
in it's entirety immediately when first element of the returned sequence is consumed.
This method performs a stable sort; that is, if the keys of two elements are equal, the order of the elements is preserved. In contrast, an unstable sort does not preserve the order of elements that have the same key.
Examples
The following code example demonstrates how to get the top N items of a sequence using DensePartialSort
.
var sequence = new[]
{
(key: 5, text: "1"),
(key: 5, text: "2"),
(key: 4, text: "3"),
(key: 4, text: "4"),
(key: 3, text: "5"),
(key: 3, text: "6"),
(key: 2, text: "7"),
(key: 2, text: "8"),
(key: 1, text: "9"),
(key: 1, text: "10"),
};
// Get the top N sets of items
var result = sequence
.DensePartialSortBy(
3,
x => x.key,
OrderByDirection.Descending);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [(5, 1), (5, 2), (4, 3), (4, 4), (3, 5), (3, 6)]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentOutOfRangeException |
|
DensePartialSortBy<TSource, TKey>(IEnumerable<TSource>, int, Func<TSource, TKey>, IComparer<TKey>?)
Executes a partial sort of the top count
elements of a sequence, including ties,
according to the key for each element, using comparer
to compare the keys. If count
is less than the total number of elements in source
, then this method will
improve performance.
Declaration
public static IEnumerable<TSource> DensePartialSortBy<TSource, TKey>(this IEnumerable<TSource> source, int count, Func<TSource, TKey> keySelector, IComparer<TKey>? comparer)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | The source sequence. |
int | count | Number of (maximum) elements to return. |
Func<TSource, TKey> | keySelector | A function to extract a key from an element. |
IComparer<TKey> | comparer | A IComparer<T> to compare elements. |
Returns
Type | Description |
---|---|
IEnumerable<TSource> | A sequence containing at most top |
Type Parameters
Name | Description |
---|---|
TSource | Type of elements in the sequence. |
TKey | Type of keys. |
Remarks
This is an O(n * log(K))
operation where K
is count
.
This method is implemented by using deferred execution. However, source
will be consumed
in it's entirety immediately when first element of the returned sequence is consumed.
This method performs a stable sort; that is, if the keys of two elements are equal, the order of the elements is preserved. In contrast, an unstable sort does not preserve the order of elements that have the same key.
Examples
The following code example demonstrates how to get the top N items of a sequence using DensePartialSort
.
var sequence = new[]
{
(key: 5, text: "1"),
(key: 5, text: "2"),
(key: 4, text: "3"),
(key: 4, text: "4"),
(key: 3, text: "5"),
(key: 3, text: "6"),
(key: 2, text: "7"),
(key: 2, text: "8"),
(key: 1, text: "9"),
(key: 1, text: "10"),
};
// Get the top N sets of items
var result = sequence
.DensePartialSortBy(
1,
x => x.key,
Comparer<int>.Create((x, y) => (x % 2).CompareTo(y % 2)));
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [(4, 3), (4, 4), (2, 7), (2, 8)]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentOutOfRangeException |
|
DensePartialSortBy<TSource, TKey>(IEnumerable<TSource>, int, Func<TSource, TKey>, IComparer<TKey>?, OrderByDirection)
Executes a direction
partial sort of the top count
elements of a
sequence, including ties, according to the key for each element, using comparer
to
compare the keys. If count
is less than the total number of elements in source
, then this method will improve performance.
Declaration
public static IEnumerable<TSource> DensePartialSortBy<TSource, TKey>(this IEnumerable<TSource> source, int count, Func<TSource, TKey> keySelector, IComparer<TKey>? comparer, OrderByDirection direction)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | The source sequence. |
int | count | Number of (maximum) elements to return. |
Func<TSource, TKey> | keySelector | A function to extract a key from an element. |
IComparer<TKey> | comparer | A IComparer<T> to compare elements. |
OrderByDirection | direction | The direction in which to sort the elements |
Returns
Type | Description |
---|---|
IEnumerable<TSource> | A sequence containing at most top |
Type Parameters
Name | Description |
---|---|
TSource | Type of elements in the sequence. |
TKey | Type of keys. |
Remarks
This is an O(n * log(K))
operation where K
is count
.
This method is implemented by using deferred execution. However, source
will be consumed
in it's entirety immediately when first element of the returned sequence is consumed.
This method performs a stable sort; that is, if the keys of two elements are equal, the order of the elements is preserved. In contrast, an unstable sort does not preserve the order of elements that have the same key.
Examples
The following code example demonstrates how to get the top N items of a sequence using DensePartialSort
.
var sequence = new[]
{
(key: 5, text: "1"),
(key: 5, text: "2"),
(key: 4, text: "3"),
(key: 4, text: "4"),
(key: 3, text: "5"),
(key: 3, text: "6"),
(key: 2, text: "7"),
(key: 2, text: "8"),
(key: 1, text: "9"),
(key: 1, text: "10"),
};
// Get the top N sets of items
var result = sequence
.DensePartialSortBy(
1,
x => x.key,
Comparer<int>.Create((x, y) => (x % 2).CompareTo(y % 2)),
OrderByDirection.Descending);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [(5, 1), (5, 2), (3, 5), (3, 6), (1, 9), (1, 10)]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentOutOfRangeException |
|