diff --git "a/Fels\303\266kning.Tests/CollectionExtensionsTests.cs" "b/Fels\303\266kning.Tests/CollectionExtensionsTests.cs" index 552dd49..81688c6 100644 --- "a/Fels\303\266kning.Tests/CollectionExtensionsTests.cs" +++ "b/Fels\303\266kning.Tests/CollectionExtensionsTests.cs" @@ -52,5 +52,155 @@ public async Task EnumerableOfType_ToAsyncEnumerable_ShouldReturnIAsyncEnumerabl item.Should().BeGreaterThan(0); } } + + [TestMethod] + public void CollectionOfType_OrderByChained_ShouldReturnIOrderedEnumerable() + { + var list = new Collection + { + new() { Id = 1, Completed = true, Title = "A Test Title", UserId = 0 }, + new() { Id = 2, Completed = true, Title = "A Test Title", UserId = 0 }, + new() { Id = 2, Completed = true, Title = "B Test Title", UserId = 1 }, + }; + + var sut = (ICollection)list; + var result = sut.OrderByChained([x => x.Id, x => x.UserId]); + result.Should().NotBeEmpty(); + var resultingList = result.ToList(); + var firstRow = resultingList[0]; + firstRow.Id.Should().Be(1); + firstRow.UserId.Should().Be(0); + var secondRow = resultingList[1]; + secondRow.Id.Should().Be(2); + secondRow.UserId.Should().Be(0); + var thirdRow = resultingList[2]; + thirdRow.Id.Should().Be(2); + thirdRow.UserId.Should().Be(1); + } + + [TestMethod] + public void EnumerableOfType_OrderByChained_ShouldReturnIOrderedEnumerable() + { + var list = new List + { + new() { Id = 1, Completed = true, Title = "A Test Title", UserId = 0 }, + new() { Id = 2, Completed = true, Title = "A Test Title", UserId = 0 }, + new() { Id = 2, Completed = true, Title = "B Test Title", UserId = 1 }, + }; + + var sut = (IEnumerable)list; + + var result = sut.OrderByChained([x => x.Id, x => x.UserId]); + result.Should().NotBeEmpty(); + var resultingList = result.ToList(); + var firstRow = resultingList[0]; + firstRow.Id.Should().Be(1); + firstRow.UserId.Should().Be(0); + var secondRow = resultingList[1]; + secondRow.Id.Should().Be(2); + secondRow.UserId.Should().Be(0); + var thirdRow = resultingList[2]; + thirdRow.Id.Should().Be(2); + thirdRow.UserId.Should().Be(1); + } + + [TestMethod] + public void ListOfType_OrderByChained_ShouldReturnIOrderedEnumerable() + { + var list = new List + { + new() { Id = 1, Completed = true, Title = "A Test Title", UserId = 0 }, + new() { Id = 2, Completed = true, Title = "A Test Title", UserId = 0 }, + new() { Id = 2, Completed = true, Title = "B Test Title", UserId = 1 }, + }; + + var result = list.OrderByChained([x => x.Id, x => x.UserId]); + result.Should().NotBeEmpty(); + var resultingList = result.ToList(); + var firstRow = resultingList[0]; + firstRow.Id.Should().Be(1); + firstRow.UserId.Should().Be(0); + var secondRow = resultingList[1]; + secondRow.Id.Should().Be(2); + secondRow.UserId.Should().Be(0); + var thirdRow = resultingList[2]; + thirdRow.Id.Should().Be(2); + thirdRow.UserId.Should().Be(1); + } + + [TestMethod] + public void CollectionOfType_OrderByDescendingChained_ShouldReturnIOrderedEnumerable() + { + var list = new Collection + { + new() { Id = 1, Completed = true, Title = "A Test Title", UserId = 0 }, + new() { Id = 2, Completed = true, Title = "A Test Title", UserId = 0 }, + new() { Id = 2, Completed = true, Title = "B Test Title", UserId = 1 }, + }; + + var sut = (ICollection)list; + var result = sut.OrderByDescendingChained([x => x.Id, x => x.UserId]); + result.Should().NotBeEmpty(); + var resultingList = result.ToList(); + var firstRow = resultingList[0]; + firstRow.Id.Should().Be(2); + firstRow.UserId.Should().Be(1); + var secondRow = resultingList[1]; + secondRow.Id.Should().Be(2); + secondRow.UserId.Should().Be(0); + var thirdRow = resultingList[2]; + thirdRow.Id.Should().Be(1); + thirdRow.UserId.Should().Be(0); + } + + [TestMethod] + public void EnumerableOfType_OrderByDescendingChained_ShouldReturnIOrderedEnumerable() + { + var list = new List + { + new() { Id = 1, Completed = true, Title = "A Test Title", UserId = 0 }, + new() { Id = 2, Completed = true, Title = "A Test Title", UserId = 0 }, + new() { Id = 2, Completed = true, Title = "B Test Title", UserId = 1 }, + }; + + var sut = (IEnumerable)list; + + var result = sut.OrderByDescendingChained([x => x.Id, x => x.UserId]); + result.Should().NotBeEmpty(); + var resultingList = result.ToList(); + var firstRow = resultingList[0]; + firstRow.Id.Should().Be(2); + firstRow.UserId.Should().Be(1); + var secondRow = resultingList[1]; + secondRow.Id.Should().Be(2); + secondRow.UserId.Should().Be(0); + var thirdRow = resultingList[2]; + thirdRow.Id.Should().Be(1); + thirdRow.UserId.Should().Be(0); + } + + [TestMethod] + public void ListOfType_OrderByDescendingChained_ShouldReturnIOrderedEnumerable() + { + var list = new List + { + new() { Id = 1, Completed = true, Title = "A Test Title", UserId = 0 }, + new() { Id = 2, Completed = true, Title = "A Test Title", UserId = 0 }, + new() { Id = 2, Completed = true, Title = "B Test Title", UserId = 1 }, + }; + + var result = list.OrderByDescendingChained([x => x.Id, x => x.UserId]); + result.Should().NotBeEmpty(); + var resultingList = result.ToList(); + var firstRow = resultingList[0]; + firstRow.Id.Should().Be(2); + firstRow.UserId.Should().Be(1); + var secondRow = resultingList[1]; + secondRow.Id.Should().Be(2); + secondRow.UserId.Should().Be(0); + var thirdRow = resultingList[2]; + thirdRow.Id.Should().Be(1); + thirdRow.UserId.Should().Be(0); + } } } \ No newline at end of file diff --git "a/Fels\303\266kning/CollectionExtensions.cs" "b/Fels\303\266kning/CollectionExtensions.cs" index fd9b0aa..2188c25 100644 --- "a/Fels\303\266kning/CollectionExtensions.cs" +++ "b/Fels\303\266kning/CollectionExtensions.cs" @@ -55,5 +55,125 @@ public static async IAsyncEnumerable ToIAsyncEnumerable(this IList valu yield return item; } } + + /// + /// Sorts the elements of a sequence in ascending order. + /// + /// The type of the elements of source. + /// The type of the key returned by keySelector. + /// A sequence of values to order. + /// An array of functions to extract a key from an element. + /// An whose elements are sorted in ascending order according to multiple keys. + public static IOrderedEnumerable OrderByChained(this ICollection source, Func[] keySelectors) + { + IOrderedEnumerable result = source.OrderBy(keySelectors[0]); + foreach (var keySelector in keySelectors) + { + // For some reason, if we don't do Func[0], again, it is ignored. + result = result.CreateOrderedEnumerable(keySelector, null, false); + } + + return result; + } + + /// + /// Sorts the elements of a sequence in ascending order. + /// + /// The type of the elements of source. + /// The type of the key returned by keySelector. + /// A sequence of values to order. + /// An array of functions to extract a key from an element. + /// An whose elements are sorted in ascending order according to multiple keys. + public static IOrderedEnumerable OrderByChained(this IEnumerable source, Func[] keySelectors) + { + IOrderedEnumerable result = source.OrderBy(keySelectors[0]); + foreach (var keySelector in keySelectors) + { + // For some reason, if we don't do Func[0], again, it is ignored. + result = result.CreateOrderedEnumerable(keySelector, null, false); + } + + return result; + } + + /// + /// Sorts the elements of a sequence in ascending order. + /// + /// The type of the elements of source. + /// The type of the key returned by keySelector. + /// A sequence of values to order. + /// An array of functions to extract a key from an element. + /// An whose elements are sorted in ascending order according to multiple keys. + public static IOrderedEnumerable OrderByChained(this IList source, Func[] keySelectors) + { + IOrderedEnumerable result = source.OrderBy(keySelectors[0]); + foreach (var keySelector in keySelectors) + { + // For some reason, if we don't do Func[0], again, it is ignored. + result = result.CreateOrderedEnumerable(keySelector, null, false); + } + + return result; + } + + /// + /// Sorts the elements of a sequence in descending order. + /// + /// The type of the elements of source. + /// The type of the key returned by keySelector. + /// A sequence of values to order. + /// An array of functions to extract a key from an element. + /// An whose elements are sorted in descending order according to multiple keys. + public static IOrderedEnumerable OrderByDescendingChained(this ICollection source, Func[] keySelectors) + { + IOrderedEnumerable result = source.OrderByDescending(keySelectors[0]); + foreach (var keySelector in keySelectors) + { + // For some reason, if we don't do Func[0], again, it is ignored. + result = result.CreateOrderedEnumerable(keySelector, null, true); + } + + return result; + } + + /// + /// Sorts the elements of a sequence in descending order. + /// + /// The type of the elements of source. + /// The type of the key returned by keySelector. + /// A sequence of values to order. + /// An array of functions to extract a key from an element. + /// An whose elements are sorted in descending order according to multiple keys. + public static IOrderedEnumerable OrderByDescendingChained(this IEnumerable source, Func[] keySelectors) + { + IOrderedEnumerable result = source.OrderByDescending(keySelectors[0]); + foreach (var keySelector in keySelectors) + { + // For some reason, if we don't do Func[0], again, it is ignored. + result = result.CreateOrderedEnumerable(keySelector, null, true); + } + + return result; + } + + /// + /// Sorts the elements of a sequence in descending order. + /// + /// The type of the elements of source. + /// The type of the key returned by keySelector. + /// A sequence of values to order. + /// An array of functions to extract a key from an element. + /// An whose elements are sorted in descending order according to multiple keys. + public static IOrderedEnumerable OrderByDescendingChained(this IList source, Func[] keySelectors) + { + IOrderedEnumerable result = source.OrderByDescending(keySelectors[0]); + foreach (var keySelector in keySelectors) + { + // For some reason, if we don't do Func[0], again, it is ignored. + result = result.CreateOrderedEnumerable(keySelector, null, true); + } + + return result; + } } } \ No newline at end of file