-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
30698c7
commit d942ec2
Showing
13 changed files
with
101 additions
and
30 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
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
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
4 changes: 2 additions & 2 deletions
4
...e/Messaging/Publishing/ISortedBehavior.cs → src/Silverback.Core/ISorted.cs
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
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
File renamed without changes.
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,23 @@ | ||
// Copyright (c) 2019 Sergio Aquilini | ||
// This code is licensed under MIT license (see LICENSE file for details) | ||
|
||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Silverback.Util | ||
{ | ||
internal static class EnumerableSortExtensions | ||
{ | ||
public static IEnumerable<T> SortBySortIndex<T>(this IEnumerable<T> items) | ||
{ | ||
var sorted = items.OfType<ISorted>().OrderBy(b => b.SortIndex).ToList(); | ||
var unsortable = items.Where(b => !(b is ISorted)).ToList(); | ||
|
||
return sorted | ||
.Where(b => b.SortIndex <= 0).Cast<T>() | ||
.Union(unsortable) | ||
.Union(sorted.Where(b => b.SortIndex > 0).Cast<T>()) | ||
.ToList(); | ||
} | ||
} | ||
} |
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
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
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
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
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
53 changes: 53 additions & 0 deletions
53
tests/Silverback.Core.Tests/Util/EnumerableSortExtensionsTests.cs
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,53 @@ | ||
// Copyright (c) 2019 Sergio Aquilini | ||
// This code is licensed under MIT license (see LICENSE file for details) | ||
|
||
using System.Collections.Generic; | ||
using FluentAssertions; | ||
using Silverback.Messaging.Publishing; | ||
using Silverback.Tests.Core.TestTypes.Behaviors; | ||
using Silverback.Tests.Core.TestTypes.Messages; | ||
using Silverback.Util; | ||
using Xunit; | ||
|
||
namespace Silverback.Tests.Core.Util | ||
{ | ||
public class EnumerableSortExtensionsTests | ||
{ | ||
[Fact] | ||
public void SortBySortIndex_SomeItems_SortedAsExpected() | ||
{ | ||
var items = new[] | ||
{ | ||
new SortedItem(100), | ||
new SortedItem(-50), | ||
new SortedItem(50), | ||
new SortedItem(-100), | ||
new Item("unsorted3"), | ||
new Item("unsorted2") | ||
}; | ||
|
||
var sorted = items.SortBySortIndex(); | ||
|
||
sorted.Should().BeEquivalentTo( | ||
new SortedItem(-100), | ||
new SortedItem(-50), | ||
new Item("unsorted3"), | ||
new Item("unsorted2"), | ||
new SortedItem(50), | ||
new SortedItem(100) | ||
); | ||
} | ||
|
||
private class Item | ||
{ | ||
public Item(string id) => Id = id; | ||
public string Id { get; } | ||
} | ||
|
||
private class SortedItem : Item, ISorted | ||
{ | ||
public SortedItem(int sortIndex) : base(sortIndex.ToString()) => SortIndex = sortIndex; | ||
public int SortIndex { get;} | ||
} | ||
} | ||
} |