mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-16 21:24:58 +00:00
feat: Adding AreOrdered enumerable extension methods
This commit is contained in:
committed by
Chris Cameron
parent
38c24d42a7
commit
52229c1472
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
|
|||||||
|
|
||||||
### Added
|
### Added
|
||||||
- IcdEnvironment.GetUtcTime() to get UTC representaiton of current time.
|
- 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
|
## [9.9.0] - 2019-09-16
|
||||||
### Added
|
### Added
|
||||||
|
|||||||
@@ -284,6 +284,15 @@ namespace ICD.Common.Utils.Tests.Extensions
|
|||||||
Assert.AreEqual(5, items.Length);
|
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]
|
[Test]
|
||||||
public void OrderTest()
|
public void OrderTest()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -648,6 +648,50 @@ namespace ICD.Common.Utils.Extensions
|
|||||||
yield return default(T);
|
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>
|
/// <summary>
|
||||||
/// Default ordering for the items in the sequence.
|
/// Default ordering for the items in the sequence.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user