diff --git a/ShittyLINQ/ThenBy.cs b/ShittyLINQ/ThenBy.cs new file mode 100644 index 0000000..f2909e3 --- /dev/null +++ b/ShittyLINQ/ThenBy.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace ShittyLINQ +{ + public static partial class Extensions + { + public static IOrderedEnumerable ThenBy(this IOrderedEnumerable source, Func keySelector) + { + if (source == null) throw new ArgumentNullException("source"); + return source.CreateOrderedEnumerable(keySelector, null, false); + } + + public static IOrderedEnumerable ThenBy(this IOrderedEnumerable source, Func keySelector, IComparer comparer) + { + if (source == null) throw new ArgumentNullException("source"); + return source.CreateOrderedEnumerable(keySelector, comparer, false); + } + } +} diff --git a/ShittyLINQ/ThenByDescending.cs b/ShittyLINQ/ThenByDescending.cs new file mode 100644 index 0000000..7083ce6 --- /dev/null +++ b/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 ThenByDescending(this IOrderedEnumerable source, Func keySelector) + { + if (source == null) throw new ArgumentNullException("source"); + return source.CreateOrderedEnumerable(keySelector, null, true); + } + + public static IOrderedEnumerable ThenByDescending(this IOrderedEnumerable source, Func keySelector, IComparer comparer) + { + if (source == null) throw new ArgumentNullException("source"); + return source.CreateOrderedEnumerable(keySelector, comparer, true); + } + } +} diff --git a/ShittyLinqTests/ThenByDescendingTests.cs b/ShittyLinqTests/ThenByDescendingTests.cs new file mode 100644 index 0000000..88b55ac --- /dev/null +++ b/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); + } + } + } +} diff --git a/ShittyLinqTests/ThenByTests.cs b/ShittyLinqTests/ThenByTests.cs new file mode 100644 index 0000000..010b0a5 --- /dev/null +++ b/ShittyLinqTests/ThenByTests.cs @@ -0,0 +1,66 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using ShittyLINQ; +using System; + +namespace ShittyTests +{ + [TestClass] + public class ThenByTests + { + + [TestMethod] + public void ThenBy_WithoutComparer() + { + // Arrange + var items = System.Linq.Enumerable.OrderBy(new (string prop1, int prop2)[] + { + ("test", 3), + ("test", 2), + ("test", 1), + }, i => i.prop1); + + // Act + var result = items.ThenBy(i => i.prop2); + + // Assert + Assert.IsNotNull(result); + using (var enumerator = result.GetEnumerator()) + { + enumerator.MoveNext(); + Assert.AreEqual(1, enumerator.Current.prop2); + enumerator.MoveNext(); + Assert.AreEqual(2, enumerator.Current.prop2); + enumerator.MoveNext(); + Assert.AreEqual(3, enumerator.Current.prop2); + } + } + + + [TestMethod] + public void ThenBy_WithComparer() + { + // Arrange + var items = System.Linq.Enumerable.OrderBy(new (string prop1, string prop2)[] + { + ("test", "c"), + ("test", "B"), + ("test", "a"), + }, i => i.prop1); + + // Act + var result = items.ThenBy(i => i.prop2, StringComparer.OrdinalIgnoreCase); + + // Assert + Assert.IsNotNull(result); + using (var enumerator = result.GetEnumerator()) + { + enumerator.MoveNext(); + Assert.AreEqual("a", enumerator.Current.prop2); + enumerator.MoveNext(); + Assert.AreEqual("B", enumerator.Current.prop2); + enumerator.MoveNext(); + Assert.AreEqual("c", enumerator.Current.prop2); + } + } + } +}