OrderBy.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. using System.Threading;
  6. using System.Threading.Tasks;
  7. namespace System.Linq
  8. {
  9. public static partial class AsyncEnumerable
  10. {
  11. /// <summary>
  12. /// Sorts the elements of a sequence in ascending order according to a key.
  13. /// </summary>
  14. /// <typeparam name="TSource">The type of the elements of source.</typeparam>
  15. /// <typeparam name="TKey">The type of the key returned by keySelector.</typeparam>
  16. /// <param name="source">An async-enumerable sequence of values to order.</param>
  17. /// <param name="keySelector">A function to extract a key from an element.</param>
  18. /// <returns>An ordered async-enumerable sequence whose elements are sorted according to a key.</returns>
  19. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> is null.</exception>
  20. public static IOrderedAsyncEnumerable<TSource> OrderBy<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector) =>
  21. new OrderedAsyncEnumerable<TSource, TKey>(source, keySelector, comparer: null, descending: false, parent: null);
  22. /// <summary>
  23. /// Sorts the elements of a sequence in ascending order according to a key obtained by invoking a transform function on each element and awaiting the result.
  24. /// </summary>
  25. /// <typeparam name="TSource">The type of the elements of source.</typeparam>
  26. /// <typeparam name="TKey">The type of the key returned by keySelector.</typeparam>
  27. /// <param name="source">An async-enumerable sequence of values to order.</param>
  28. /// <param name="keySelector">An asynchronous function to extract a key from an element.</param>
  29. /// <returns>An ordered async-enumerable sequence whose elements are sorted according to a key.</returns>
  30. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> is null.</exception>
  31. [GenerateAsyncOverload]
  32. private static IOrderedAsyncEnumerable<TSource> OrderByAwaitCore<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector) =>
  33. new OrderedAsyncEnumerableWithTask<TSource, TKey>(source, keySelector, comparer: null, descending: false, parent: null);
  34. #if !NO_DEEP_CANCELLATION
  35. [GenerateAsyncOverload]
  36. private static IOrderedAsyncEnumerable<TSource> OrderByAwaitWithCancellationCore<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector) =>
  37. new OrderedAsyncEnumerableWithTaskAndCancellation<TSource, TKey>(source, keySelector, comparer: null, descending: false, parent: null);
  38. #endif
  39. /// <summary>
  40. /// Sorts the elements of a sequence in ascending order by using a specified comparer.
  41. /// </summary>
  42. /// <typeparam name="TSource">The type of the elements of source.</typeparam>
  43. /// <typeparam name="TKey">The type of the key returned by keySelector.</typeparam>
  44. /// <param name="source">An async-enumerable sequence of values to order.</param>
  45. /// <param name="keySelector">A function to extract a key from an element.</param>
  46. /// <param name="comparer">A comparer to compare keys.</param>
  47. /// <returns>An ordered async-enumerable sequence whose elements are sorted according to a key.</returns>
  48. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> is null.</exception>
  49. public static IOrderedAsyncEnumerable<TSource> OrderBy<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer) =>
  50. new OrderedAsyncEnumerable<TSource, TKey>(source, keySelector, comparer, descending: false, parent: null);
  51. /// <summary>
  52. /// Sorts the elements of a sequence in ascending order by using a specified comparer. The keys are obtained by invoking the transform function on each element and awaiting the result.
  53. /// </summary>
  54. /// <typeparam name="TSource">The type of the elements of source.</typeparam>
  55. /// <typeparam name="TKey">The type of the key returned by keySelector.</typeparam>
  56. /// <param name="source">An async-enumerable sequence of values to order.</param>
  57. /// <param name="keySelector">An asynchronous function to extract a key from an element.</param>
  58. /// <param name="comparer">A comparer to compare keys.</param>
  59. /// <returns>An ordered async-enumerable sequence whose elements are sorted according to a key.</returns>
  60. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> is null.</exception>
  61. [GenerateAsyncOverload]
  62. private static IOrderedAsyncEnumerable<TSource> OrderByAwaitCore<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, IComparer<TKey> comparer) =>
  63. new OrderedAsyncEnumerableWithTask<TSource, TKey>(source, keySelector, comparer, descending: false, parent: null);
  64. #if !NO_DEEP_CANCELLATION
  65. [GenerateAsyncOverload]
  66. private static IOrderedAsyncEnumerable<TSource> OrderByAwaitWithCancellationCore<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, IComparer<TKey> comparer) =>
  67. new OrderedAsyncEnumerableWithTaskAndCancellation<TSource, TKey>(source, keySelector, comparer, descending: false, parent: null);
  68. #endif
  69. /// <summary>
  70. /// Sorts the elements of a sequence in descending order according to a key.
  71. /// </summary>
  72. /// <typeparam name="TSource">The type of the elements of source.</typeparam>
  73. /// <typeparam name="TKey">The type of the key returned by keySelector.</typeparam>
  74. /// <param name="source">An async-enumerable sequence of values to order.</param>
  75. /// <param name="keySelector">A function to extract a key from an element.</param>
  76. /// <returns>An ordered async-enumerable sequence whose elements are sorted in descending order according to a key.</returns>
  77. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> is null.</exception>
  78. public static IOrderedAsyncEnumerable<TSource> OrderByDescending<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector) =>
  79. new OrderedAsyncEnumerable<TSource, TKey>(source, keySelector, comparer: null, descending: true, parent: null);
  80. /// <summary>
  81. /// Sorts the elements of a sequence in descending order according to a key obtained by invoking a transform function on each element and awaiting the result.
  82. /// </summary>
  83. /// <typeparam name="TSource">The type of the elements of source.</typeparam>
  84. /// <typeparam name="TKey">The type of the key returned by keySelector.</typeparam>
  85. /// <param name="source">An async-enumerable sequence of values to order.</param>
  86. /// <param name="keySelector">An asynchronous function to extract a key from an element.</param>
  87. /// <returns>An ordered async-enumerable sequence whose elements are sorted in descending order according to a key.</returns>
  88. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> is null.</exception>
  89. [GenerateAsyncOverload]
  90. private static IOrderedAsyncEnumerable<TSource> OrderByDescendingAwaitCore<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector) =>
  91. new OrderedAsyncEnumerableWithTask<TSource, TKey>(source, keySelector, comparer: null, descending: true, parent: null);
  92. #if !NO_DEEP_CANCELLATION
  93. [GenerateAsyncOverload]
  94. private static IOrderedAsyncEnumerable<TSource> OrderByDescendingAwaitWithCancellationCore<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector) =>
  95. new OrderedAsyncEnumerableWithTaskAndCancellation<TSource, TKey>(source, keySelector, comparer: null, descending: true, parent: null);
  96. #endif
  97. /// <summary>
  98. /// Sorts the elements of a sequence in descending order by using a specified comparer.
  99. /// </summary>
  100. /// <typeparam name="TSource">The type of the elements of source.</typeparam>
  101. /// <typeparam name="TKey">The type of the key returned by keySelector.</typeparam>
  102. /// <param name="source">An async-enumerable sequence of values to order.</param>
  103. /// <param name="keySelector">A function to extract a key from an element.</param>
  104. /// <param name="comparer">A comparer to compare keys.</param>
  105. /// <returns>An ordered async-enumerable sequence whose elements are sorted in descending order according to a key.</returns>
  106. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> is null.</exception>
  107. public static IOrderedAsyncEnumerable<TSource> OrderByDescending<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer) =>
  108. new OrderedAsyncEnumerable<TSource, TKey>(source, keySelector, comparer, descending: true, parent: null);
  109. /// <summary>
  110. /// Sorts the elements of a sequence in descending order by using a specified comparer. The keys are obtained by invoking the transform function on each element and awaiting the result.
  111. /// </summary>
  112. /// <typeparam name="TSource">The type of the elements of source.</typeparam>
  113. /// <typeparam name="TKey">The type of the key returned by keySelector.</typeparam>
  114. /// <param name="source">An async-enumerable sequence of values to order.</param>
  115. /// <param name="keySelector">An asynchronous function to extract a key from an element.</param>
  116. /// <param name="comparer">A comparer to compare keys.</param>
  117. /// <returns>An ordered async-enumerable sequence whose elements are sorted in descending order according to a key.</returns>
  118. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> is null.</exception>
  119. [GenerateAsyncOverload]
  120. private static IOrderedAsyncEnumerable<TSource> OrderByDescendingAwaitCore<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, IComparer<TKey> comparer) =>
  121. new OrderedAsyncEnumerableWithTask<TSource, TKey>(source, keySelector, comparer, descending: true, parent: null);
  122. #if !NO_DEEP_CANCELLATION
  123. [GenerateAsyncOverload]
  124. private static IOrderedAsyncEnumerable<TSource> OrderByDescendingAwaitWithCancellationCore<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, IComparer<TKey> comparer) =>
  125. new OrderedAsyncEnumerableWithTaskAndCancellation<TSource, TKey>(source, keySelector, comparer, descending: true, parent: null);
  126. #endif
  127. /// <summary>
  128. /// Performs a subsequent ordering of the elements in a sequence in ascending order according to a key.
  129. /// </summary>
  130. /// <typeparam name="TSource">The type of the elements of source.</typeparam>
  131. /// <typeparam name="TKey">The type of the key returned by keySelector.</typeparam>
  132. /// <param name="source">An ordered async-enumerable sequence that contains elements to sort.</param>
  133. /// <param name="keySelector">A function to extract a key from each element.</param>
  134. /// <returns>An ordered async-enumerable whose elements are sorted according to a key.</returns>
  135. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> is null.</exception>
  136. public static IOrderedAsyncEnumerable<TSource> ThenBy<TSource, TKey>(this IOrderedAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector)
  137. {
  138. if (source == null)
  139. throw Error.ArgumentNull(nameof(source));
  140. return source.CreateOrderedEnumerable(keySelector, comparer: null, descending: false);
  141. }
  142. /// <summary>
  143. /// Performs a subsequent ordering of the elements in a sequence in ascending order according to a key obtained by invoking a transform function on each element and awaiting the result.
  144. /// </summary>
  145. /// <typeparam name="TSource">The type of the elements of source.</typeparam>
  146. /// <typeparam name="TKey">The type of the key returned by keySelector.</typeparam>
  147. /// <param name="source">An ordered async-enumerable sequence that contains elements to sort.</param>
  148. /// <param name="keySelector">An asynchronous function to extract a key from each element.</param>
  149. /// <returns>An ordered async-enumerable whose elements are sorted according to a key.</returns>
  150. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> is null.</exception>
  151. [GenerateAsyncOverload]
  152. private static IOrderedAsyncEnumerable<TSource> ThenByAwaitCore<TSource, TKey>(this IOrderedAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector)
  153. {
  154. if (source == null)
  155. throw Error.ArgumentNull(nameof(source));
  156. return source.CreateOrderedEnumerable(keySelector, comparer: default(IComparer<TKey>), descending: false);
  157. }
  158. #if !NO_DEEP_CANCELLATION
  159. [GenerateAsyncOverload]
  160. private static IOrderedAsyncEnumerable<TSource> ThenByAwaitWithCancellationCore<TSource, TKey>(this IOrderedAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector)
  161. {
  162. if (source == null)
  163. throw Error.ArgumentNull(nameof(source));
  164. return source.CreateOrderedEnumerable(keySelector, comparer: null, descending: false);
  165. }
  166. #endif
  167. /// <summary>
  168. /// Performs a subsequent ordering of the elements in a sequence in ascending order by using a specified comparer.
  169. /// </summary>
  170. /// <typeparam name="TSource">The type of the elements of source.</typeparam>
  171. /// <typeparam name="TKey">The type of the key returned by keySelector.</typeparam>
  172. /// <param name="source">An ordered async-enumerable sequence that contains elements to sort.</param>
  173. /// <param name="keySelector">A function to extract a key from each element.</param>
  174. /// <param name="comparer">A comparer to compare keys.</param>
  175. /// <returns>An ordered async-enumerable whose elements are sorted according to a key.</returns>
  176. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> is null.</exception>
  177. public static IOrderedAsyncEnumerable<TSource> ThenBy<TSource, TKey>(this IOrderedAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer)
  178. {
  179. if (source == null)
  180. throw Error.ArgumentNull(nameof(source));
  181. return source.CreateOrderedEnumerable(keySelector, comparer, descending: false);
  182. }
  183. /// <summary>
  184. /// Performs a subsequent ordering of the elements in a sequence in ascending order by using a specified comparer. The keys are obtained by invoking a transform function on each element and awaiting the result.
  185. /// </summary>
  186. /// <typeparam name="TSource">The type of the elements of source.</typeparam>
  187. /// <typeparam name="TKey">The type of the key returned by keySelector.</typeparam>
  188. /// <param name="source">An ordered async-enumerable sequence that contains elements to sort.</param>
  189. /// <param name="keySelector">An asynchronous function to extract a key from each element.</param>
  190. /// <param name="comparer">A comparer to compare keys.</param>
  191. /// <returns>An ordered async-enumerable whose elements are sorted according to a key.</returns>
  192. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> is null.</exception>
  193. [GenerateAsyncOverload]
  194. private static IOrderedAsyncEnumerable<TSource> ThenByAwaitCore<TSource, TKey>(this IOrderedAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, IComparer<TKey> comparer)
  195. {
  196. if (source == null)
  197. throw Error.ArgumentNull(nameof(source));
  198. return source.CreateOrderedEnumerable(keySelector, comparer, descending: false);
  199. }
  200. #if !NO_DEEP_CANCELLATION
  201. [GenerateAsyncOverload]
  202. private static IOrderedAsyncEnumerable<TSource> ThenByAwaitWithCancellationCore<TSource, TKey>(this IOrderedAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, IComparer<TKey> comparer)
  203. {
  204. if (source == null)
  205. throw Error.ArgumentNull(nameof(source));
  206. return source.CreateOrderedEnumerable(keySelector, comparer, descending: false);
  207. }
  208. #endif
  209. /// <summary>
  210. /// Performs a subsequent ordering of the elements in a sequence in descending order, according to a key.
  211. /// </summary>
  212. /// <typeparam name="TSource">The type of the elements of source.</typeparam>
  213. /// <typeparam name="TKey">The type of the key returned by keySelector.</typeparam>
  214. /// <param name="source">An ordered async-enumerable sequence that contains elements to sort.</param>
  215. /// <param name="keySelector">A function to extract a key from each element.</param>
  216. /// <returns>An ordered async-enumerable sequence whose elements are sorted in descending order according to a key.</returns>
  217. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> is null.</exception>
  218. public static IOrderedAsyncEnumerable<TSource> ThenByDescending<TSource, TKey>(this IOrderedAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector)
  219. {
  220. if (source == null)
  221. throw Error.ArgumentNull(nameof(source));
  222. return source.CreateOrderedEnumerable(keySelector, comparer: null, descending: true);
  223. }
  224. /// <summary>
  225. /// Performs a subsequent ordering of the elements in a sequence in descending order, according to a key obtained by invoking a transform function on each element and awaiting the result.
  226. /// </summary>
  227. /// <typeparam name="TSource">The type of the elements of source.</typeparam>
  228. /// <typeparam name="TKey">The type of the key returned by keySelector.</typeparam>
  229. /// <param name="source">An ordered async-enumerable sequence that contains elements to sort.</param>
  230. /// <param name="keySelector">An asynchronous function to extract a key from each element.</param>
  231. /// <returns>An ordered async-enumerable sequence whose elements are sorted in descending order according to a key.</returns>
  232. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> is null.</exception>
  233. [GenerateAsyncOverload]
  234. private static IOrderedAsyncEnumerable<TSource> ThenByDescendingAwaitCore<TSource, TKey>(this IOrderedAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector)
  235. {
  236. if (source == null)
  237. throw Error.ArgumentNull(nameof(source));
  238. return source.CreateOrderedEnumerable(keySelector, comparer: default(IComparer<TKey>), descending: true);
  239. }
  240. #if !NO_DEEP_CANCELLATION
  241. [GenerateAsyncOverload]
  242. private static IOrderedAsyncEnumerable<TSource> ThenByDescendingAwaitWithCancellationCore<TSource, TKey>(this IOrderedAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector)
  243. {
  244. if (source == null)
  245. throw Error.ArgumentNull(nameof(source));
  246. return source.CreateOrderedEnumerable(keySelector, comparer: null, descending: true);
  247. }
  248. #endif
  249. /// <summary>
  250. /// Performs a subsequent ordering of the elements in a sequence in descending order by using a specified comparer.
  251. /// </summary>
  252. /// <typeparam name="TSource">The type of the elements of source.</typeparam>
  253. /// <typeparam name="TKey">The type of the key returned by keySelector.</typeparam>
  254. /// <param name="source">An ordered async-enumerable sequence that contains elements to sort.</param>
  255. /// <param name="keySelector">A function to extract a key from each element.</param>
  256. /// <param name="comparer">A comparer to compare keys.</param>
  257. /// <returns>An ordered async-enumerable sequence whose elements are sorted in descending order according to a key.</returns>
  258. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> is null.</exception>
  259. public static IOrderedAsyncEnumerable<TSource> ThenByDescending<TSource, TKey>(this IOrderedAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer)
  260. {
  261. if (source == null)
  262. throw Error.ArgumentNull(nameof(source));
  263. return source.CreateOrderedEnumerable(keySelector, comparer, descending: true);
  264. }
  265. /// <summary>
  266. /// Performs a subsequent ordering of the elements in a sequence in descending order by using a specified comparer. The keys are obtained by invoking a transform function on each element and awaiting the result.
  267. /// </summary>
  268. /// <typeparam name="TSource">The type of the elements of source.</typeparam>
  269. /// <typeparam name="TKey">The type of the key returned by keySelector.</typeparam>
  270. /// <param name="source">An ordered async-enumerable sequence that contains elements to sort.</param>
  271. /// <param name="keySelector">An asynchronous function to extract a key from each element.</param>
  272. /// <param name="comparer">A comparer to compare keys.</param>
  273. /// <returns>An ordered async-enumerable sequence whose elements are sorted in descending order according to a key.</returns>
  274. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="keySelector"/> is null.</exception>
  275. [GenerateAsyncOverload]
  276. private static IOrderedAsyncEnumerable<TSource> ThenByDescendingAwaitCore<TSource, TKey>(this IOrderedAsyncEnumerable<TSource> source, Func<TSource, ValueTask<TKey>> keySelector, IComparer<TKey> comparer)
  277. {
  278. if (source == null)
  279. throw Error.ArgumentNull(nameof(source));
  280. return source.CreateOrderedEnumerable(keySelector, comparer, descending: true);
  281. }
  282. #if !NO_DEEP_CANCELLATION
  283. [GenerateAsyncOverload]
  284. private static IOrderedAsyncEnumerable<TSource> ThenByDescendingAwaitWithCancellationCore<TSource, TKey>(this IOrderedAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<TKey>> keySelector, IComparer<TKey> comparer)
  285. {
  286. if (source == null)
  287. throw Error.ArgumentNull(nameof(source));
  288. return source.CreateOrderedEnumerable(keySelector, comparer, descending: true);
  289. }
  290. #endif
  291. }
  292. }