Min.cs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. {
  20. throw new ArgumentNullException(nameof(source));
  21. }
  22. if (comparer == null)
  23. {
  24. throw new ArgumentNullException(nameof(comparer));
  25. }
  26. return MinBy(source, x => x, comparer)
  27. .First();
  28. }
  29. /// <summary>
  30. /// Returns the elements with the minimum key value by using the default comparer to compare key values.
  31. /// </summary>
  32. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  33. /// <typeparam name="TKey">Key type.</typeparam>
  34. /// <param name="source">Source sequence.</param>
  35. /// <param name="keySelector">Key selector used to extract the key for each element in the sequence.</param>
  36. /// <returns>List with the elements that share the same minimum key value.</returns>
  37. public static IList<TSource> MinBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
  38. {
  39. if (source == null)
  40. {
  41. throw new ArgumentNullException(nameof(source));
  42. }
  43. if (keySelector == null)
  44. {
  45. throw new ArgumentNullException(nameof(keySelector));
  46. }
  47. return MinBy(source, keySelector, Comparer<TKey>.Default);
  48. }
  49. /// <summary>
  50. /// Returns the elements with the minimum key value by using the specified comparer to compare key values.
  51. /// </summary>
  52. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  53. /// <typeparam name="TKey">Key type.</typeparam>
  54. /// <param name="source">Source sequence.</param>
  55. /// <param name="keySelector">Key selector used to extract the key for each element in the sequence.</param>
  56. /// <param name="comparer">Comparer used to determine the minimum key value.</param>
  57. /// <returns>List with the elements that share the same minimum key value.</returns>
  58. public static IList<TSource> MinBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer)
  59. {
  60. if (source == null)
  61. {
  62. throw new ArgumentNullException(nameof(source));
  63. }
  64. if (keySelector == null)
  65. {
  66. throw new ArgumentNullException(nameof(keySelector));
  67. }
  68. if (comparer == null)
  69. {
  70. throw new ArgumentNullException(nameof(comparer));
  71. }
  72. return ExtremaBy(source, keySelector, (key, minValue) => -comparer.Compare(key, minValue));
  73. }
  74. }
  75. }