Skip to content

Commit

Permalink
ThenByDescending extension method and unit test implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
jojosardez committed Oct 3, 2018
1 parent 5d33197 commit 41ae7f5
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
21 changes: 21 additions & 0 deletions ShittyLINQ/ThenByDescending.cs
@@ -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);
}
}
}
64 changes: 64 additions & 0 deletions ShittyLinqTests/ThenByDescendingTests.cs
@@ -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);
}
}
}
}

0 comments on commit 41ae7f5

Please sign in to comment.