MaxBy.cs 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT 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. #if !(REFERENCE_ASSEMBLY && (NET6_0))
  10. /// <summary>
  11. /// Returns the elements with the maximum key value by using the default comparer to compare key values.
  12. /// </summary>
  13. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  14. /// <typeparam name="TKey">Key type.</typeparam>
  15. /// <param name="source">Source sequence.</param>
  16. /// <param name="keySelector">Key selector used to extract the key for each element in the sequence.</param>
  17. /// <returns>List with the elements that share the same maximum key value.</returns>
  18. public static IList<TSource> MaxBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
  19. {
  20. if (source == null)
  21. throw new ArgumentNullException(nameof(source));
  22. if (keySelector == null)
  23. throw new ArgumentNullException(nameof(keySelector));
  24. return MaxBy(source, keySelector, Comparer<TKey>.Default);
  25. }
  26. /// <summary>
  27. /// Returns the elements with the minimum key value by using the specified comparer to compare key values.
  28. /// </summary>
  29. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  30. /// <typeparam name="TKey">Key type.</typeparam>
  31. /// <param name="source">Source sequence.</param>
  32. /// <param name="keySelector">Key selector used to extract the key for each element in the sequence.</param>
  33. /// <param name="comparer">Comparer used to determine the maximum key value.</param>
  34. /// <returns>List with the elements that share the same maximum key value.</returns>
  35. public static IList<TSource> MaxBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer)
  36. {
  37. if (source == null)
  38. throw new ArgumentNullException(nameof(source));
  39. if (keySelector == null)
  40. throw new ArgumentNullException(nameof(keySelector));
  41. if (comparer == null)
  42. throw new ArgumentNullException(nameof(comparer));
  43. return ExtremaBy(source, keySelector, (key, minValue) => comparer.Compare(key, minValue));
  44. }
  45. private static IList<TSource> ExtremaBy<TSource, TKey>(IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TKey, TKey, int> compare)
  46. {
  47. var result = new List<TSource>();
  48. using (var e = source.GetEnumerator())
  49. {
  50. if (!e.MoveNext())
  51. throw new InvalidOperationException("Source sequence doesn't contain any elements.");
  52. var current = e.Current;
  53. var resKey = keySelector(current);
  54. result.Add(current);
  55. while (e.MoveNext())
  56. {
  57. var cur = e.Current;
  58. var key = keySelector(cur);
  59. var cmp = compare(key, resKey);
  60. if (cmp == 0)
  61. {
  62. result.Add(cur);
  63. }
  64. else if (cmp > 0)
  65. {
  66. result = new List<TSource> { cur };
  67. resKey = key;
  68. }
  69. }
  70. }
  71. return result;
  72. }
  73. #endif
  74. }
  75. }