Method PartialSort
| Edit this page View SourcePartialSort<T>(IEnumerable<T>, int)
Executes a partial sort of the top count
elements of a sequence. If count
is less than the total number of elements in source
, then this method will
improve performance.
Declaration
public static IEnumerable<T> PartialSort<T>(this IEnumerable<T> source, int count)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | source | The source sequence. |
int | count | Number of (maximum) elements to return. |
Returns
Type | Description |
---|---|
IEnumerable<T> | A sequence containing at most top |
Type Parameters
Name | Description |
---|---|
T | Type of elements in the sequence. |
Remarks
This operation is an O(n * log(K))
where K
is count
.
This operator uses deferred execution and streams it results.
Examples
The following code example demonstrates how to get the top N items of a sequence using PartialSort
.
var sequence = new Item[]
{
new(key: 5, text: "1"),
new(key: 5, text: "2"),
new(key: 4, text: "3"),
new(key: 4, text: "4"),
new(key: 3, text: "5"),
new(key: 3, text: "6"),
new(key: 2, text: "7"),
new(key: 2, text: "8"),
new(key: 1, text: "9"),
new(key: 1, text: "10"),
};
// Get the top N items
var result = sequence.PartialSort(3);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [(1, 9), (1, 10), (2, 7)]
class Item : IComparable<Item>
{
public Item(int key, string text)
{
Key = key;
Text = text;
}
public int Key { get; }
public string Text { get; }
public int CompareTo(Item other) =>
this.Key.CompareTo(other.Key);
public override string ToString() =>
$"({this.Key}, {this.Text})";
}
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentOutOfRangeException |
|
PartialSort<T>(IEnumerable<T>, int, OrderByDirection)
Executes a direction
partial sort of the top count
elements of a
sequence. If count
is less than the total number of elements in source
, then this method will improve performance.
Declaration
public static IEnumerable<T> PartialSort<T>(this IEnumerable<T> source, int count, OrderByDirection direction)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | source | The source sequence. |
int | count | Number of (maximum) elements to return. |
OrderByDirection | direction | The direction in which to sort the elements |
Returns
Type | Description |
---|---|
IEnumerable<T> | A sequence containing at most top |
Type Parameters
Name | Description |
---|---|
T | Type of elements in the sequence. |
Remarks
This operation is an O(n * log(K))
where K
is count
.
This operator uses deferred execution and streams it results.
Examples
The following code example demonstrates how to get the top N items of a sequence using PartialSort
.
var sequence = new Item[]
{
new(key: 5, text: "1"),
new(key: 5, text: "2"),
new(key: 4, text: "3"),
new(key: 4, text: "4"),
new(key: 3, text: "5"),
new(key: 3, text: "6"),
new(key: 2, text: "7"),
new(key: 2, text: "8"),
new(key: 1, text: "9"),
new(key: 1, text: "10"),
};
// Get the top N items
var result = sequence.PartialSort(3, OrderByDirection.Descending);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [(5, 1), (5, 2), (4, 3)]
class Item : IComparable<Item>
{
public Item(int key, string text)
{
Key = key;
Text = text;
}
public int Key { get; }
public string Text { get; }
public int CompareTo(Item other) =>
this.Key.CompareTo(other.Key);
public override string ToString() =>
$"({this.Key}, {this.Text})";
}
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentOutOfRangeException |
|
PartialSort<T>(IEnumerable<T>, int, IComparer<T>?)
Executes a partial sort of the top count
elements of a sequence, using comparer
to compare elements. If count
is less than the total number of elements
in source
, then this method will improve performance.
Declaration
public static IEnumerable<T> PartialSort<T>(this IEnumerable<T> source, int count, IComparer<T>? comparer)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | source | The source sequence. |
int | count | Number of (maximum) elements to return. |
IComparer<T> | comparer | A IComparer<T> to compare elements. |
Returns
Type | Description |
---|---|
IEnumerable<T> | A sequence containing at most top |
Type Parameters
Name | Description |
---|---|
T | Type of elements in the sequence. |
Remarks
This operation is an O(n * log(K))
where K
is count
.
This operator uses deferred execution and streams it results.
Examples
The following code example demonstrates how to get the top N items of a sequence using PartialSort
.
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 items
var result = sequence
.PartialSort(
3,
Comparer<(int key, string text)>.Create((x, y) => x.key.CompareTo(y.key)));
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [(1, 9), (1, 10), (2, 7)]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentOutOfRangeException |
|
PartialSort<T>(IEnumerable<T>, int, IComparer<T>?, OrderByDirection)
Executes a direction
partial sort of the top count
elements of a
sequence, using comparer
to compare elements. If count
is less than
the total number of elements in source
, then this method will improve performance.
Declaration
public static IEnumerable<T> PartialSort<T>(this IEnumerable<T> source, int count, IComparer<T>? comparer, OrderByDirection direction)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | source | The source sequence. |
int | count | Number of (maximum) elements to return. |
IComparer<T> | comparer | A IComparer<T> to compare elements. |
OrderByDirection | direction | The direction in which to sort the elements |
Returns
Type | Description |
---|---|
IEnumerable<T> | A sequence containing at most top |
Type Parameters
Name | Description |
---|---|
T | Type of elements in the sequence. |
Remarks
This operation is an O(n * log(K))
where K
is count
.
This operator uses deferred execution and streams it results.
Examples
The following code example demonstrates how to get the top N items of a sequence using PartialSort
.
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 items
var result = sequence
.PartialSort(
3,
Comparer<(int key, string text)>.Create((x, y) => x.key.CompareTo(y.key)),
OrderByDirection.Descending);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [(5, 1), (5, 2), (4, 3)]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentOutOfRangeException |
|