Skip to content

Commit 8adb9c7

Browse files
authored
Add Rank overloads with OrderByDirection (#620)
1 parent 7691a3f commit 8adb9c7

33 files changed

+2059
-588
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
uid: SuperLinq.SuperEnumerable.DenseRank``1(System.Collections.Generic.IEnumerable{``0})
3+
example: [*content]
4+
---
5+
The following code example demonstrates how to rank the items in a sequence using `DenseRank`.
6+
[!code-csharp[](SuperLinq/DenseRank/DenseRank1.linq#L6-)]
7+
8+
---
9+
uid: SuperLinq.SuperEnumerable.DenseRank``1(System.Collections.Generic.IEnumerable{``0},SuperLinq.OrderByDirection)
10+
example: [*content]
11+
---
12+
The following code example demonstrates how to rank the items in a sequence using `DenseRank`.
13+
[!code-csharp[](SuperLinq/DenseRank/DenseRank2.linq#L6-)]
14+
15+
---
16+
uid: SuperLinq.SuperEnumerable.DenseRank``1(System.Collections.Generic.IEnumerable{``0},System.Collections.Generic.IComparer{``0})
17+
example: [*content]
18+
---
19+
The following code example demonstrates how to rank the items in a sequence using `DenseRank`.
20+
[!code-csharp[](SuperLinq/DenseRank/DenseRank3.linq#L6-)]
21+
22+
---
23+
uid: SuperLinq.SuperEnumerable.DenseRank``1(System.Collections.Generic.IEnumerable{``0},System.Collections.Generic.IComparer{``0},SuperLinq.OrderByDirection)
24+
example: [*content]
25+
---
26+
The following code example demonstrates how to rank the items in a sequence using `DenseRank`.
27+
[!code-csharp[](SuperLinq/DenseRank/DenseRank4.linq#L6-)]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
uid: SuperLinq.SuperEnumerable.DenseRankBy``2(System.Collections.Generic.IEnumerable{``0},System.Func{``0,``1})
3+
example: [*content]
4+
---
5+
The following code example demonstrates how to rank the items in a sequence according to a key using `DenseRankBy`.
6+
[!code-csharp[](SuperLinq/DenseRankBy/DenseRankBy1.linq#L6-)]
7+
8+
---
9+
uid: SuperLinq.SuperEnumerable.DenseRankBy``2(System.Collections.Generic.IEnumerable{``0},System.Func{``0,``1},SuperLinq.OrderByDirection)
10+
example: [*content]
11+
---
12+
The following code example demonstrates how to rank the items in a sequence according to a key using `DenseRankBy`.
13+
[!code-csharp[](SuperLinq/DenseRankBy/DenseRankBy2.linq#L6-)]
14+
15+
---
16+
uid: SuperLinq.SuperEnumerable.DenseRankBy``2(System.Collections.Generic.IEnumerable{``0},System.Func{``0,``1},System.Collections.Generic.IComparer{``1})
17+
example: [*content]
18+
---
19+
The following code example demonstrates how to rank the items in a sequence according to a key using `DenseRankBy`.
20+
[!code-csharp[](SuperLinq/DenseRankBy/DenseRankBy3.linq#L6-)]
21+
22+
---
23+
uid: SuperLinq.SuperEnumerable.DenseRankBy``2(System.Collections.Generic.IEnumerable{``0},System.Func{``0,``1},System.Collections.Generic.IComparer{``1},SuperLinq.OrderByDirection)
24+
example: [*content]
25+
---
26+
The following code example demonstrates how to rank the items in a sequence according to a key using `DenseRankBy`.
27+
[!code-csharp[](SuperLinq/DenseRankBy/DenseRankBy4.linq#L6-)]
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,27 @@
1-
---
2-
uid: SuperLinq.SuperEnumerable.DenseRank``1(System.Collections.Generic.IEnumerable{``0})
3-
example: [*content]
4-
---
5-
The following code example demonstrates how to rank the items in a sequence using `DenseRank`.
6-
[!code-csharp[](SuperLinq/DenseRank/DenseRank1.linq#L6-)]
7-
8-
---
9-
uid: SuperLinq.SuperEnumerable.DenseRank``1(System.Collections.Generic.IEnumerable{``0},System.Collections.Generic.IComparer{``0})
10-
example: [*content]
11-
---
12-
The following code example demonstrates how to rank the items in a sequence using `DenseRank`.
13-
[!code-csharp[](SuperLinq/DenseRank/DenseRank3.linq#L6-)]
14-
15-
---
16-
uid: SuperLinq.SuperEnumerable.DenseRankBy``2(System.Collections.Generic.IEnumerable{``0},System.Func{``0,``1})
17-
example: [*content]
18-
---
19-
The following code example demonstrates how to rank the items in a sequence according to a key using `DenseRankBy`.
20-
[!code-csharp[](SuperLinq/DenseRankBy/DenseRankBy1.linq#L6-)]
21-
22-
---
23-
uid: SuperLinq.SuperEnumerable.DenseRankBy``2(System.Collections.Generic.IEnumerable{``0},System.Func{``0,``1},System.Collections.Generic.IComparer{``1})
24-
example: [*content]
25-
---
26-
The following code example demonstrates how to rank the items in a sequence according to a key using `DenseRankBy`.
27-
[!code-csharp[](SuperLinq/DenseRankBy/DenseRankBy3.linq#L6-)]
28-
29-
----
1+
----
302
uid: SuperLinq.SuperEnumerable.Rank``1(System.Collections.Generic.IEnumerable{``0})
313
example: [*content]
324
----
335
The following code example demonstrates how to rank the items in a sequence using `Rank`.
346
[!code-csharp[](SuperLinq/Rank/Rank1.linq#L6-)]
357

368
----
37-
uid: SuperLinq.SuperEnumerable.Rank``1(System.Collections.Generic.IEnumerable{``0},System.Collections.Generic.IComparer{``0})
9+
uid: SuperLinq.SuperEnumerable.Rank``1(System.Collections.Generic.IEnumerable{``0},SuperLinq.OrderByDirection)
3810
example: [*content]
3911
----
4012
The following code example demonstrates how to rank the items in a sequence using `Rank`.
41-
[!code-csharp[](SuperLinq/Rank/Rank3.linq#L6-)]
13+
[!code-csharp[](SuperLinq/Rank/Rank2.linq#L6-)]
4214

4315
----
44-
uid: SuperLinq.SuperEnumerable.RankBy``2(System.Collections.Generic.IEnumerable{``0},System.Func{``0,``1})
16+
uid: SuperLinq.SuperEnumerable.Rank``1(System.Collections.Generic.IEnumerable{``0},System.Collections.Generic.IComparer{``0})
4517
example: [*content]
4618
----
47-
The following code example demonstrates how to rank the items in a sequence according to a key using `RankBy`.
48-
[!code-csharp[](SuperLinq/RankBy/RankBy1.linq#L6-)]
19+
The following code example demonstrates how to rank the items in a sequence using `Rank`.
20+
[!code-csharp[](SuperLinq/Rank/Rank3.linq#L6-)]
4921

5022
----
51-
uid: SuperLinq.SuperEnumerable.RankBy``2(System.Collections.Generic.IEnumerable{``0},System.Func{``0,``1},System.Collections.Generic.IComparer{``1})
23+
uid: SuperLinq.SuperEnumerable.Rank``1(System.Collections.Generic.IEnumerable{``0},System.Collections.Generic.IComparer{``0},SuperLinq.OrderByDirection)
5224
example: [*content]
5325
----
54-
The following code example demonstrates how to rank the items in a sequence according to a key using `RankBy`.
55-
[!code-csharp[](SuperLinq/RankBy/RankBy3.linq#L6-)]
26+
The following code example demonstrates how to rank the items in a sequence using `Rank`.
27+
[!code-csharp[](SuperLinq/Rank/Rank4.linq#L6-)]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
----
2+
uid: SuperLinq.SuperEnumerable.RankBy``2(System.Collections.Generic.IEnumerable{``0},System.Func{``0,``1})
3+
example: [*content]
4+
----
5+
The following code example demonstrates how to rank the items in a sequence according to a key using `RankBy`.
6+
[!code-csharp[](SuperLinq/RankBy/RankBy1.linq#L6-)]
7+
8+
----
9+
uid: SuperLinq.SuperEnumerable.RankBy``2(System.Collections.Generic.IEnumerable{``0},System.Func{``0,``1},SuperLinq.OrderByDirection)
10+
example: [*content]
11+
----
12+
The following code example demonstrates how to rank the items in a sequence according to a key using `RankBy`.
13+
[!code-csharp[](SuperLinq/RankBy/RankBy2.linq#L6-)]
14+
15+
----
16+
uid: SuperLinq.SuperEnumerable.RankBy``2(System.Collections.Generic.IEnumerable{``0},System.Func{``0,``1},System.Collections.Generic.IComparer{``1})
17+
example: [*content]
18+
----
19+
The following code example demonstrates how to rank the items in a sequence according to a key using `RankBy`.
20+
[!code-csharp[](SuperLinq/RankBy/RankBy3.linq#L6-)]
21+
22+
----
23+
uid: SuperLinq.SuperEnumerable.RankBy``2(System.Collections.Generic.IEnumerable{``0},System.Func{``0,``1},System.Collections.Generic.IComparer{``1},SuperLinq.OrderByDirection)
24+
example: [*content]
25+
----
26+
The following code example demonstrates how to rank the items in a sequence according to a key using `RankBy`.
27+
[!code-csharp[](SuperLinq/RankBy/RankBy4.linq#L6-)]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<Query Kind="Statements">
2+
<NuGetReference>SuperLinq</NuGetReference>
3+
<Namespace>SuperLinq</Namespace>
4+
</Query>
5+
6+
var sequence = new Item[]
7+
{
8+
new(key: 5, text: "1"),
9+
new(key: 5, text: "2"),
10+
new(key: 4, text: "3"),
11+
new(key: 4, text: "4"),
12+
new(key: 3, text: "5"),
13+
new(key: 3, text: "6"),
14+
new(key: 2, text: "7"),
15+
new(key: 2, text: "8"),
16+
new(key: 1, text: "9"),
17+
new(key: 1, text: "10"),
18+
};
19+
20+
// Rank the items in the sequence
21+
var result = sequence.DenseRank(OrderByDirection.Ascending);
22+
23+
Console.WriteLine(
24+
"[" +
25+
string.Join(", ", result) +
26+
"]");
27+
28+
// This code produces the following output:
29+
// [((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)]
30+
31+
class Item : IComparable<Item>
32+
{
33+
public Item(int key, string text)
34+
{
35+
Key = key;
36+
Text = text;
37+
}
38+
39+
public int Key { get; }
40+
public string Text { get; }
41+
42+
public int CompareTo(Item other) =>
43+
this.Key.CompareTo(other.Key);
44+
45+
public override string ToString() =>
46+
$"({this.Key}, {this.Text})";
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<Query Kind="Statements">
2+
<NuGetReference>SuperLinq</NuGetReference>
3+
<Namespace>SuperLinq</Namespace>
4+
</Query>
5+
6+
var sequence = new[]
7+
{
8+
(key: 5, text: "1"),
9+
(key: 5, text: "2"),
10+
(key: 4, text: "3"),
11+
(key: 4, text: "4"),
12+
(key: 3, text: "5"),
13+
(key: 3, text: "6"),
14+
(key: 2, text: "7"),
15+
(key: 2, text: "8"),
16+
(key: 1, text: "9"),
17+
(key: 1, text: "10"),
18+
};
19+
20+
// Get the top N sets of items
21+
var result = sequence
22+
.DenseRank(
23+
Comparer<(int key, string text)>.Create((x, y) => x.key.CompareTo(y.key)),
24+
OrderByDirection.Ascending);
25+
26+
Console.WriteLine(
27+
"[" +
28+
string.Join(", ", result) +
29+
"]");
30+
31+
// This code produces the following output:
32+
// [((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)]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<Query Kind="Statements">
2+
<NuGetReference>SuperLinq</NuGetReference>
3+
<Namespace>SuperLinq</Namespace>
4+
</Query>
5+
6+
var sequence = new[]
7+
{
8+
(key: 5, text: "1"),
9+
(key: 5, text: "2"),
10+
(key: 4, text: "3"),
11+
(key: 4, text: "4"),
12+
(key: 3, text: "5"),
13+
(key: 3, text: "6"),
14+
(key: 2, text: "7"),
15+
(key: 2, text: "8"),
16+
(key: 1, text: "9"),
17+
(key: 1, text: "10"),
18+
};
19+
20+
// Get the top N sets of items
21+
var result = sequence
22+
.DenseRankBy(
23+
x => x.key,
24+
OrderByDirection.Ascending);
25+
26+
Console.WriteLine(
27+
"[" +
28+
string.Join(", ", result) +
29+
"]");
30+
31+
// This code produces the following output:
32+
// [((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)]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<Query Kind="Statements">
2+
<NuGetReference>SuperLinq</NuGetReference>
3+
<Namespace>SuperLinq</Namespace>
4+
</Query>
5+
6+
var sequence = new[]
7+
{
8+
(key: 5, text: "1"),
9+
(key: 5, text: "2"),
10+
(key: 4, text: "3"),
11+
(key: 4, text: "4"),
12+
(key: 3, text: "5"),
13+
(key: 3, text: "6"),
14+
(key: 2, text: "7"),
15+
(key: 2, text: "8"),
16+
(key: 1, text: "9"),
17+
(key: 1, text: "10"),
18+
};
19+
20+
// Get the top N sets of items
21+
var result = sequence
22+
.DenseRankBy(
23+
x => x.key,
24+
Comparer<int>.Create((x, y) => (x % 2).CompareTo(y % 2)),
25+
OrderByDirection.Ascending);
26+
27+
Console.WriteLine(
28+
"[" +
29+
string.Join(", ", result) +
30+
"]");
31+
32+
// This code produces the following output:
33+
// [((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)]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<Query Kind="Statements">
2+
<NuGetReference>SuperLinq</NuGetReference>
3+
<Namespace>SuperLinq</Namespace>
4+
</Query>
5+
6+
var sequence = new Item[]
7+
{
8+
new(key: 5, text: "1"),
9+
new(key: 5, text: "2"),
10+
new(key: 4, text: "3"),
11+
new(key: 4, text: "4"),
12+
new(key: 3, text: "5"),
13+
new(key: 3, text: "6"),
14+
new(key: 2, text: "7"),
15+
new(key: 2, text: "8"),
16+
new(key: 1, text: "9"),
17+
new(key: 1, text: "10"),
18+
};
19+
20+
// Rank the items in the sequence
21+
var result = sequence.Rank(OrderByDirection.Ascending);
22+
23+
Console.WriteLine(
24+
"[" +
25+
string.Join(", ", result) +
26+
"]");
27+
28+
// This code produces the following output:
29+
// [((1, 9), 1), ((1, 10), 1), ((2, 7), 3), ((2, 8), 3), ((3, 5), 5), ((3, 6), 5), ((4, 3), 7), ((4, 4), 7), ((5, 1), 9), ((5, 2), 9)]
30+
31+
class Item : IComparable<Item>
32+
{
33+
public Item(int key, string text)
34+
{
35+
Key = key;
36+
Text = text;
37+
}
38+
39+
public int Key { get; }
40+
public string Text { get; }
41+
42+
public int CompareTo(Item other) =>
43+
this.Key.CompareTo(other.Key);
44+
45+
public override string ToString() =>
46+
$"({this.Key}, {this.Text})";
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<Query Kind="Statements">
2+
<NuGetReference>SuperLinq</NuGetReference>
3+
<Namespace>SuperLinq</Namespace>
4+
</Query>
5+
6+
var sequence = new[]
7+
{
8+
(key: 5, text: "1"),
9+
(key: 5, text: "2"),
10+
(key: 4, text: "3"),
11+
(key: 4, text: "4"),
12+
(key: 3, text: "5"),
13+
(key: 3, text: "6"),
14+
(key: 2, text: "7"),
15+
(key: 2, text: "8"),
16+
(key: 1, text: "9"),
17+
(key: 1, text: "10"),
18+
};
19+
20+
// Rank the items in the sequence
21+
var result = sequence
22+
.Rank(
23+
Comparer<(int key, string text)>.Create((x, y) => x.key.CompareTo(y.key)),
24+
OrderByDirection.Ascending);
25+
26+
Console.WriteLine(
27+
"[" +
28+
string.Join(", ", result) +
29+
"]");
30+
31+
// This code produces the following output:
32+
// [((1, 9), 1), ((1, 10), 1), ((2, 7), 3), ((2, 8), 3), ((3, 5), 5), ((3, 6), 5), ((4, 3), 7), ((4, 4), 7), ((5, 1), 9), ((5, 2), 9)]

0 commit comments

Comments
 (0)