forked from ShittyLabs/ShittyLINQ
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ThenByDescending extension method and unit test implementation.
- Loading branch information
1 parent
5d33197
commit 41ae7f5
Showing
2 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace ShittyLINQ | ||
{ | ||
public static partial class Extensions | ||
{ | ||
public static IOrderedEnumerable<TSource> ThenByDescending<TSource, TKey>(this IOrderedEnumerable<TSource> source, Func<TSource, TKey> keySelector) | ||
{ | ||
if (source == null) throw new ArgumentNullException("source"); | ||
return source.CreateOrderedEnumerable<TKey>(keySelector, null, true); | ||
} | ||
|
||
public static IOrderedEnumerable<TSource> ThenByDescending<TSource, TKey>(this IOrderedEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer) | ||
{ | ||
if (source == null) throw new ArgumentNullException("source"); | ||
return source.CreateOrderedEnumerable<TKey>(keySelector, comparer, true); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using ShittyLINQ; | ||
using System; | ||
|
||
namespace ShittyTests | ||
{ | ||
[TestClass] | ||
public class ThenByDescendingTests | ||
{ | ||
[TestMethod] | ||
public void ThenByDescending_WithoutComparer() | ||
{ | ||
// Arrange | ||
var items = System.Linq.Enumerable.OrderBy(new (string prop1, int prop2)[] | ||
{ | ||
("test", 1), | ||
("test", 2), | ||
("test", 3), | ||
}, i => i.prop1); | ||
|
||
// Act | ||
var result = items.ThenByDescending(i => i.prop2); | ||
|
||
// Assert | ||
Assert.IsNotNull(result); | ||
using (var enumerator = result.GetEnumerator()) | ||
{ | ||
enumerator.MoveNext(); | ||
Assert.AreEqual(3, enumerator.Current.prop2); | ||
enumerator.MoveNext(); | ||
Assert.AreEqual(2, enumerator.Current.prop2); | ||
enumerator.MoveNext(); | ||
Assert.AreEqual(1, enumerator.Current.prop2); | ||
} | ||
} | ||
|
||
[TestMethod] | ||
public void ThenByDescending_WithComparer() | ||
{ | ||
// Arrange | ||
var items = System.Linq.Enumerable.OrderBy(new (string prop1, string prop2)[] | ||
{ | ||
("test", "a"), | ||
("test", "B"), | ||
("test", "c"), | ||
}, i => i.prop1); | ||
|
||
// Act | ||
var result = items.ThenByDescending(i => i.prop2, StringComparer.OrdinalIgnoreCase); | ||
|
||
// Assert | ||
Assert.IsNotNull(result); | ||
using (var enumerator = result.GetEnumerator()) | ||
{ | ||
enumerator.MoveNext(); | ||
Assert.AreEqual("c", enumerator.Current.prop2); | ||
enumerator.MoveNext(); | ||
Assert.AreEqual("B", enumerator.Current.prop2); | ||
enumerator.MoveNext(); | ||
Assert.AreEqual("a", enumerator.Current.prop2); | ||
} | ||
} | ||
} | ||
} |