1
0

Min.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. namespace System.Linq
  6. {
  7. public static partial class EnumerableEx
  8. {
  9. /// <summary>
  10. /// Returns the minimum value in the enumerable sequence by using the specified comparer to compare values.
  11. /// </summary>
  12. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  13. /// <param name="source">Source sequence.</param>
  14. /// <param name="comparer">Comparer used to determine the minimum value.</param>
  15. /// <returns>Minimum value in the sequence.</returns>
  16. public static TSource Min<TSource>(this IEnumerable<TSource> source, IComparer<TSource> comparer)
  17. {
  18. if (source == null)
  19. throw new ArgumentNullException(nameof(source));
  20. if (comparer == null)
  21. throw new ArgumentNullException(nameof(comparer));
  22. return MinBy(source, x => x, comparer).First();
  23. }
  24. /// <summary>
  25. /// Returns the elements with the minimum key value by using the default comparer to compare key values.
  26. /// </summary>
  27. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  28. /// <typeparam name="TKey">Key type.</typeparam>
  29. /// <param name="source">Source sequence.</param>
  30. /// <param name="keySelector">Key selector used to extract the key for each element in the sequence.</param>
  31. /// <returns>List with the elements that share the same minimum key value.</returns>
  32. public static IList<TSource> MinBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
  33. {
  34. if (source == null)
  35. throw new ArgumentNullException(nameof(source));
  36. if (keySelector == null)
  37. throw new ArgumentNullException(nameof(keySelector));
  38. return MinBy(source, keySelector, Comparer<TKey>.Default);
  39. }
  40. /// <summary>
  41. /// Returns the elements with the minimum key value by using the specified comparer to compare key values.
  42. /// </summary>
  43. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  44. /// <typeparam name="TKey">Key type.</typeparam>
  45. /// <param name="source">Source sequence.</param>
  46. /// <param name="keySelector">Key selector used to extract the key for each element in the sequence.</param>
  47. /// <param name="comparer">Comparer used to determine the minimum key value.</param>
  48. /// <returns>List with the elements that share the same minimum key value.</returns>
  49. public static IList<TSource> MinBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer)
  50. {
  51. if (source == null)
  52. throw new ArgumentNullException(nameof(source));
  53. if (keySelector == null)
  54. throw new ArgumentNullException(nameof(keySelector));
  55. if (comparer == null)
  56. throw new ArgumentNullException(nameof(comparer));
  57. return ExtremaBy(source, keySelector, (key, minValue) => -comparer.Compare(key, minValue));
  58. }
  59. }
  60. }