Method DenseRankBy
| Edit this pageDenseRankBy<TSource, TKey>(IEnumerable<TSource>, Func<TSource, TKey>)
Ranks each item in the sequence in ascending order by a specified key using a default comparer, with no gaps in the ranking values. The rank starts at one and keeps incrementing by one for each different item.
Declaration
public static IEnumerable<(TSource item, int rank)> DenseRankBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<TSource> | source | The sequence of items to rank |
| Func<TSource, TKey> | keySelector | A key selector function which returns the value by which to rank items in the sequence |
Returns
| Type | Description |
|---|---|
| IEnumerable<(TSource item, int rank)> | A sorted sequence of items and their rank. |
Type Parameters
| Name | Description |
|---|---|
| TSource | The type of the elements in the source sequence |
| TKey | The type of the key used to rank items in the sequence |
Remarks
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.
Examples
The following code example demonstrates how to rank the items in a sequence according to a key using DenseRankBy.
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
.DenseRankBy(
x => x.key);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [((1, 9), 1), ((1, 10), 1), ((2, 7), 2), ((2, 8), 2), ((3, 5), 3), ((3, 6), 3), ((4, 3), 4), ((4, 4), 4), ((5, 1), 5), ((5, 2), 5)]
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
DenseRankBy<TSource, TKey>(IEnumerable<TSource>, Func<TSource, TKey>, OrderByDirection)
Ranks each item in the sequence in the order defined by sortDirection by a specified key
using a default comparer, with no gaps in the ranking values. The rank starts at one and keeps incrementing
by one for each different item.
Declaration
public static IEnumerable<(TSource item, int rank)> DenseRankBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, OrderByDirection sortDirection)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<TSource> | source | The sequence of items to rank |
| Func<TSource, TKey> | keySelector | A key selector function which returns the value by which to rank items in the sequence |
| OrderByDirection | sortDirection | Defines the ordering direction for the sequence |
Returns
| Type | Description |
|---|---|
| IEnumerable<(TSource item, int rank)> | A sorted sequence of items and their rank. |
Type Parameters
| Name | Description |
|---|---|
| TSource | The type of the elements in the source sequence |
| TKey | The type of the key used to rank items in the sequence |
Remarks
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.
Examples
The following code example demonstrates how to rank the items in a sequence according to a key using DenseRankBy.
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
.DenseRankBy(
x => x.key,
OrderByDirection.Ascending);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [((1, 9), 1), ((1, 10), 1), ((2, 7), 2), ((2, 8), 2), ((3, 5), 3), ((3, 6), 3), ((4, 3), 4), ((4, 4), 4), ((5, 1), 5), ((5, 2), 5)]
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
DenseRankBy<TSource, TKey>(IEnumerable<TSource>, Func<TSource, TKey>, IComparer<TKey>)
Ranks each item in the sequence in ascending order by a specified key using a caller-supplied comparer, with no gaps in the ranking values. The rank starts at one and keeps incrementing by one for each different item.
Declaration
public static IEnumerable<(TSource item, int rank)> DenseRankBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<TSource> | source | The sequence of items to rank |
| Func<TSource, TKey> | keySelector | A key selector function which returns the value by which to rank items in the sequence |
| IComparer<TKey> | comparer | An object that defines the comparison semantics for keys used to rank items |
Returns
| Type | Description |
|---|---|
| IEnumerable<(TSource item, int rank)> | A sorted sequence of items and their rank. |
Type Parameters
| Name | Description |
|---|---|
| TSource | The type of the elements in the source sequence |
| TKey | The type of the key used to rank items in the sequence |
Remarks
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.
Examples
The following code example demonstrates how to rank the items in a sequence according to a key using DenseRankBy.
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
.DenseRankBy(
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), 1), ((4, 4), 1), ((2, 7), 1), ((2, 8), 1), ((5, 1), 2), ((5, 2), 2), ((3, 5), 2), ((3, 6), 2), ((1, 9), 2), ((1, 10), 2)]
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
DenseRankBy<TSource, TKey>(IEnumerable<TSource>, Func<TSource, TKey>, IComparer<TKey>, OrderByDirection)
Ranks each item in the sequence in the order defined by sortDirection by a specified key
using a caller-supplied comparer, with no gaps in the ranking values. The rank starts at one and keeps
incrementing by one for each different item.
Declaration
public static IEnumerable<(TSource item, int rank)> DenseRankBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer, OrderByDirection sortDirection)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<TSource> | source | The sequence of items to rank |
| Func<TSource, TKey> | keySelector | A key selector function which returns the value by which to rank items in the sequence |
| IComparer<TKey> | comparer | An object that defines the comparison semantics for keys used to rank items |
| OrderByDirection | sortDirection | Defines the ordering direction for the sequence |
Returns
| Type | Description |
|---|---|
| IEnumerable<(TSource item, int rank)> | A sorted sequence of items and their rank. |
Type Parameters
| Name | Description |
|---|---|
| TSource | The type of the elements in the source sequence |
| TKey | The type of the key used to rank items in the sequence |
Remarks
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.
Examples
The following code example demonstrates how to rank the items in a sequence according to a key using DenseRankBy.
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
.DenseRankBy(
x => x.key,
Comparer<int>.Create((x, y) => (x % 2).CompareTo(y % 2)),
OrderByDirection.Ascending);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [((4, 3), 1), ((4, 4), 1), ((2, 7), 1), ((2, 8), 1), ((5, 1), 2), ((5, 2), 2), ((3, 5), 2), ((3, 6), 2), ((1, 9), 2), ((1, 10), 2)]
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|