Min.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. namespace System.Linq
  9. {
  10. public static partial class EnumerableEx
  11. {
  12. /// <summary>
  13. /// Returns the minimum value in the enumerable sequence by using the specified comparer to compare values.
  14. /// </summary>
  15. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  16. /// <param name="source">Source sequence.</param>
  17. /// <param name="comparer">Comparer used to determine the minimum value.</param>
  18. /// <returns>Minimum value in the sequence.</returns>
  19. public static TSource Min<TSource>(this IEnumerable<TSource> source, IComparer<TSource> comparer)
  20. {
  21. if (source == null)
  22. throw new ArgumentNullException(nameof(source));
  23. if (comparer == null)
  24. throw new ArgumentNullException(nameof(comparer));
  25. return MinBy(source, x => x, comparer)
  26. .First();
  27. }
  28. /// <summary>
  29. /// Returns the elements with the minimum key value by using the default comparer to compare key values.
  30. /// </summary>
  31. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  32. /// <typeparam name="TKey">Key type.</typeparam>
  33. /// <param name="source">Source sequence.</param>
  34. /// <param name="keySelector">Key selector used to extract the key for each element in the sequence.</param>
  35. /// <returns>List with the elements that share the same minimum key value.</returns>
  36. public static IList<TSource> MinBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
  37. {
  38. if (source == null)
  39. throw new ArgumentNullException(nameof(source));
  40. if (keySelector == null)
  41. throw new ArgumentNullException(nameof(keySelector));
  42. return MinBy(source, keySelector, Comparer<TKey>.Default);
  43. }
  44. /// <summary>
  45. /// Returns the elements with the minimum key value by using the specified comparer to compare key values.
  46. /// </summary>
  47. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  48. /// <typeparam name="TKey">Key type.</typeparam>
  49. /// <param name="source">Source sequence.</param>
  50. /// <param name="keySelector">Key selector used to extract the key for each element in the sequence.</param>
  51. /// <param name="comparer">Comparer used to determine the minimum key value.</param>
  52. /// <returns>List with the elements that share the same minimum key value.</returns>
  53. public static IList<TSource> MinBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer)
  54. {
  55. if (source == null)
  56. throw new ArgumentNullException(nameof(source));
  57. if (keySelector == null)
  58. throw new ArgumentNullException(nameof(keySelector));
  59. if (comparer == null)
  60. throw new ArgumentNullException(nameof(comparer));
  61. return ExtremaBy(source, keySelector, (key, minValue) => -comparer.Compare(key, minValue));
  62. }
  63. }
  64. }