mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-04-12 12:07:05 +00:00
feat: Added MinOrDefault extension method
This commit is contained in:
parent
022e625143
commit
6c6d272511
1 changed files with 34 additions and 7 deletions
|
|
@ -1305,6 +1305,37 @@ namespace ICD.Common.Utils.Extensions
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns the minimum value from the sequence, otherwise the default value.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T"></typeparam>
|
||||||
|
/// <param name="extends"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static T MinOrDefault<T>([NotNull] this IEnumerable<T> extends)
|
||||||
|
{
|
||||||
|
if (extends == null)
|
||||||
|
throw new ArgumentNullException("extends");
|
||||||
|
|
||||||
|
Comparer<T> comparer = Comparer<T>.Default;
|
||||||
|
T value = default(T);
|
||||||
|
bool first = true;
|
||||||
|
|
||||||
|
foreach (T x in extends)
|
||||||
|
{
|
||||||
|
if (first)
|
||||||
|
{
|
||||||
|
first = false;
|
||||||
|
value = x;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (comparer.Compare(x, value) < 0)
|
||||||
|
value = x;
|
||||||
|
}
|
||||||
|
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns the maximum value from the sequence, otherwise the default value.
|
/// Returns the maximum value from the sequence, otherwise the default value.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
@ -1319,14 +1350,10 @@ namespace ICD.Common.Utils.Extensions
|
||||||
Comparer<T> comparer = Comparer<T>.Default;
|
Comparer<T> comparer = Comparer<T>.Default;
|
||||||
T value = default(T);
|
T value = default(T);
|
||||||
|
|
||||||
using (IEnumerator<T> enumerator = extends.GetEnumerator())
|
foreach (T x in extends)
|
||||||
{
|
{
|
||||||
while (enumerator.MoveNext())
|
if (comparer.Compare(x, value) > 0)
|
||||||
{
|
value = x;
|
||||||
T x = enumerator.Current;
|
|
||||||
if (comparer.Compare(x, value) > 0)
|
|
||||||
value = x;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return value;
|
return value;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue