feat: Adding AreOrdered enumerable extension methods

This commit is contained in:
Chris Cameron
2019-09-29 21:56:19 -04:00
committed by Chris Cameron
parent 38c24d42a7
commit 52229c1472
3 changed files with 54 additions and 0 deletions

View File

@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Added
- IcdEnvironment.GetUtcTime() to get UTC representaiton of current time.
- Extension methods for determining if a sequence is in order
## [9.9.0] - 2019-09-16
### Added

View File

@@ -284,6 +284,15 @@ namespace ICD.Common.Utils.Tests.Extensions
Assert.AreEqual(5, items.Length);
}
[TestCase(true)]
[TestCase(true, 1, 2, 3, 4)]
[TestCase(true, 1, 2, 2, 3, 4)]
[TestCase(false, 4, 3, 2, 1)]
public void AreOrderedTest(bool expected, params int[] items)
{
Assert.AreEqual(expected, items.AreOrdered());
}
[Test]
public void OrderTest()
{

View File

@@ -648,6 +648,50 @@ namespace ICD.Common.Utils.Extensions
yield return default(T);
}
/// <summary>
/// Returns true if the given sequence is ordered.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="extends"></param>
/// <returns></returns>
public static bool AreOrdered<T>(this IEnumerable<T> extends)
{
if (extends == null)
throw new ArgumentNullException("extends");
return extends.AreOrdered(Comparer<T>.Default);
}
/// <summary>
/// Returns true if the given sequence is ordered.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="extends"></param>
/// <param name="comparer"></param>
/// <returns></returns>
public static bool AreOrdered<T>(this IEnumerable<T> extends, IComparer<T> comparer)
{
if (extends == null)
throw new ArgumentNullException("extends");
if (comparer == null)
throw new ArgumentNullException("comparer");
bool first = true;
T previous = default(T);
foreach (T item in extends)
{
if (!first && comparer.Compare(item, previous) < 0)
return false;
first = false;
previous = item;
}
return true;
}
/// <summary>
/// Default ordering for the items in the sequence.
/// </summary>