Method DenseRank
| Edit this pageDenseRank<TSource>(IEnumerable<TSource>)
Ranks each item in the sequence in ascending order 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)> DenseRank<TSource>(this IEnumerable<TSource> source)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<TSource> | source | The sequence whose items will be ranked |
Returns
| Type | Description |
|---|---|
| IEnumerable<(TSource item, int rank)> | A sorted sequence of items and their rank. |
Type Parameters
| Name | Description |
|---|---|
| TSource | Type of item 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 using DenseRank.
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"),
};
// Rank the items in the sequence
var result = sequence.DenseRank();
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)]
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 |
|
DenseRank<TSource>(IEnumerable<TSource>, OrderByDirection)
Ranks each item in the sequence in the order defined by sortDirection 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)> DenseRank<TSource>(this IEnumerable<TSource> source, OrderByDirection sortDirection)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<TSource> | source | The sequence whose items will be ranked |
| 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 | Type of item 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 using DenseRank.
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"),
};
// Rank the items in the sequence
var result = sequence.DenseRank(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)]
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 |
|
DenseRank<TSource>(IEnumerable<TSource>, IComparer<TSource>)
Ranks each item in the sequence in ascending order 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)> DenseRank<TSource>(this IEnumerable<TSource> source, IComparer<TSource> comparer)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<TSource> | source | The sequence of items to rank |
| IComparer<TSource> | comparer | A object that defines comparison semantics for the elements 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 |
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 using DenseRank.
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
.DenseRank(
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), ((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 |
|
DenseRank<TSource>(IEnumerable<TSource>, IComparer<TSource>, OrderByDirection)
Ranks each item in the sequence in the order defined by sortDirection 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)> DenseRank<TSource>(this IEnumerable<TSource> source, IComparer<TSource> comparer, OrderByDirection sortDirection)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<TSource> | source | The sequence of items to rank |
| IComparer<TSource> | comparer | A object that defines comparison semantics for the elements 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 |
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 using DenseRank.
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
.DenseRank(
Comparer<(int key, string text)>.Create((x, y) => x.key.CompareTo(y.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 |
|