PageRenderTime 3260ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Obtics/Collections/ObservableEnumerable.ThenByDescending.cs

#
C# | 130 lines | 44 code | 14 blank | 72 comment | 18 complexity | e6b8b7e0bf452a7a34126a77b1e81c0e MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using Obtics.Values;
  4. using System.Linq;
  5. namespace Obtics.Collections
  6. {
  7. public static partial class ObservableEnumerable
  8. {
  9. /// <summary>
  10. /// Performs a subsequent ordering of the elements in a sequence in descending order according to a key.
  11. /// </summary>
  12. /// <typeparam name="TSource">The type of the elements of the sequence.</typeparam>
  13. /// <typeparam name="TKey">The type of the key.</typeparam>
  14. /// <param name="source">An <see cref="IObservableOrderedEnumerable{TSource}"/> that contains elements to sort.</param>
  15. /// <param name="keySelector">A function to extract a key from each element.</param>
  16. /// <returns>An <see cref="IObservableOrderedEnumerable{TSource}"/>, whose elements are sorted according to a key, or null when either <paramref name="source"/> or <paramref name="keySelector"/> is null.</returns>
  17. public static IObservableOrderedEnumerable<TSource> ThenByDescending<TSource, TKey>(this IObservableOrderedEnumerable<TSource> source, Func<TSource, TKey> keySelector)
  18. { return ThenByDescending(source, keySelector, Comparer<TKey>.Default); }
  19. /// <summary>
  20. /// Performs a subsequent ordering of the elements in a sequence in descending order by using a given comparer.
  21. /// </summary>
  22. /// <typeparam name="TSource">The type of the elements of the sequence.</typeparam>
  23. /// <typeparam name="TKey">The type of the key.</typeparam>
  24. /// <param name="source">An <see cref="IObservableOrderedEnumerable{TSource}"/> that contains elements to sort.</param>
  25. /// <param name="keySelector">A function to extract a key from each element.</param>
  26. /// <param name="comparer">An <see cref="IComparer{TKey}"/> used to compare key values.</param>
  27. /// <returns>An <see cref="IObservableOrderedEnumerable{TSource}"/>, whose elements are sorted according to a key, or null when either <paramref name="source"/> or <paramref name="keySelector"/> is null.</returns>
  28. public static IObservableOrderedEnumerable<TSource> ThenByDescending<TSource, TKey>(this IObservableOrderedEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer)
  29. {
  30. if (source == null)
  31. return null;
  32. return source.CreateOrderedEnumerable(keySelector, comparer, true);
  33. }
  34. /// <summary>
  35. /// Performs a subsequent ordering of the elements in a sequence in descending order according to a key dynamicaly.
  36. /// </summary>
  37. /// <typeparam name="TSource">The type of the elements of the sequence.</typeparam>
  38. /// <typeparam name="TKey">The type of the key.</typeparam>
  39. /// <param name="source">An <see cref="IObservableOrderedEnumerable{TSource}"/> that contains elements to sort.</param>
  40. /// <param name="keySelector">A function to extract a key from each element. It returns an <see cref="IValueProvider{TKey}"/> whose <typeparamref name="TKey"/> Value property gives the key to order the given element by.</param>
  41. /// <returns>An <see cref="IObservableOrderedEnumerable{TSource}"/>, whose elements are sorted according to a key, or null when either <paramref name="source"/> or <paramref name="keySelector"/> is null.</returns>
  42. public static IObservableOrderedEnumerable<TSource> ThenByDescending<TSource, TKey>(this IObservableOrderedEnumerable<TSource> source, Func<TSource, IValueProvider<TKey>> keySelector)
  43. { return ThenByDescending(source, keySelector, Comparer<TKey>.Default); }
  44. /// <summary>
  45. /// Performs a subsequent ordering of the elements in a sequence in descending order dynamicaly by using a given comparer.
  46. /// </summary>
  47. /// <typeparam name="TSource">The type of the elements of the sequence.</typeparam>
  48. /// <typeparam name="TKey">The type of the key.</typeparam>
  49. /// <param name="source">An <see cref="IObservableOrderedEnumerable{TSource}"/> that contains elements to sort.</param>
  50. /// <param name="keySelector">A function to extract a key from each element.</param>
  51. /// <param name="comparer">An <see cref="IComparer{TKey}"/> used to compare key values. It returns an <see cref="IValueProvider{TKey}"/> whose <typeparamref name="TKey"/> Value property gives the key to order the given element by.</param>
  52. /// <returns>An <see cref="IObservableOrderedEnumerable{TSource}"/>, whose elements are sorted according to a key, or null when either <paramref name="source"/> or <paramref name="keySelector"/> is null.</returns>
  53. public static IObservableOrderedEnumerable<TSource> ThenByDescending<TSource, TKey>(this IObservableOrderedEnumerable<TSource> source, Func<TSource, IValueProvider<TKey>> keySelector, IComparer<TKey> comparer)
  54. {
  55. if (source == null)
  56. return null;
  57. return source.CreateOrderedEnumerable(keySelector, comparer, true);
  58. }
  59. /// <summary>
  60. /// Performs a subsequent ordering of the elements in a sequence in descending order according to a key.
  61. /// </summary>
  62. /// <typeparam name="TSource">The type of the elements of the sequence.</typeparam>
  63. /// <typeparam name="TKey">The type of the key.</typeparam>
  64. /// <param name="source">An <see cref="IOrderedEnumerable{TSource}"/> that contains elements to sort.</param>
  65. /// <param name="keySelector">A function to extract a key from each element.</param>
  66. /// <returns>An <see cref="IOrderedEnumerable{TSource}"/>, whose elements are sorted according to a key, or null when either <paramref name="source"/> or <paramref name="keySelector"/> is null.</returns>
  67. /// <remarks>The result of this method will only be reactive and observable if source is in fact an <see cref="IObservableOrderedEnumerable{TSource}"/>.</remarks>
  68. public static IOrderedEnumerable<TSource> ThenByDescending<TSource, TKey>(this IOrderedEnumerable<TSource> source, Func<TSource, TKey> keySelector)
  69. { return ThenByDescending(source, keySelector, Comparer<TKey>.Default); }
  70. /// <summary>
  71. /// Performs a subsequent ordering of the elements in a sequence in descending order by using a given comparer.
  72. /// </summary>
  73. /// <typeparam name="TSource">The type of the elements of the sequence.</typeparam>
  74. /// <typeparam name="TKey">The type of the key.</typeparam>
  75. /// <param name="source">An <see cref="IObservableOrderedEnumerable{TSource}"/> that contains elements to sort.</param>
  76. /// <param name="keySelector">A function to extract a key from each element.</param>
  77. /// <param name="comparer">An <see cref="IComparer{TKey}"/> used to compare key values.</param>
  78. /// <returns>An <see cref="IObservableOrderedEnumerable{TSource}"/>, whose elements are sorted according to a key, or null when either <paramref name="source"/> or <paramref name="keySelector"/> is null.</returns>
  79. /// <remarks>The result of this method will only be reactive and observable if source is in fact an <see cref="IObservableOrderedEnumerable{TSource}"/>.</remarks>
  80. public static IOrderedEnumerable<TSource> ThenByDescending<TSource, TKey>(this IOrderedEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer)
  81. {
  82. if (source == null || keySelector == null || comparer == null )
  83. return null;
  84. var asObservable = source as IObservableOrderedEnumerable<TSource>;
  85. return asObservable != null ? asObservable.CreateOrderedEnumerable(keySelector, comparer, true) : source.CreateOrderedEnumerable(keySelector, comparer, true);
  86. }
  87. /// <summary>
  88. /// Performs a subsequent ordering of the elements in a sequence in descending order according to a key dynamicaly.
  89. /// </summary>
  90. /// <typeparam name="TSource">The type of the elements of the sequence.</typeparam>
  91. /// <typeparam name="TKey">The type of the key.</typeparam>
  92. /// <param name="source">An <see cref="IOrderedEnumerable{TSource}"/> that contains elements to sort.</param>
  93. /// <param name="keySelector">A function to extract a key from each element. It returns an <see cref="IValueProvider{TKey}"/> whose <typeparamref name="TKey"/> Value property gives the key to order the given element by.</param>
  94. /// <returns>An <see cref="IOrderedEnumerable{TSource}"/>, whose elements are sorted according to a key, or null when either <paramref name="source"/> or <paramref name="keySelector"/> is null.</returns>
  95. /// <remarks>The result of this method will only be reactive and observable if source is in fact an <see cref="IObservableOrderedEnumerable{TSource}"/>.</remarks>
  96. public static IOrderedEnumerable<TSource> ThenByDescending<TSource, TKey>(this IOrderedEnumerable<TSource> source, Func<TSource, IValueProvider<TKey>> keySelector)
  97. { return ThenByDescending(source, keySelector, Comparer<TKey>.Default); }
  98. /// <summary>
  99. /// Performs a subsequent ordering of the elements in a sequence in descending order dynamicaly by using a given comparer.
  100. /// </summary>
  101. /// <typeparam name="TSource">The type of the elements of the sequence.</typeparam>
  102. /// <typeparam name="TKey">The type of the key.</typeparam>
  103. /// <param name="source">An <see cref="IObservableOrderedEnumerable{TSource}"/> that contains elements to sort.</param>
  104. /// <param name="keySelector">A function to extract a key from each element. It returns an <see cref="IValueProvider{TKey}"/> whose <typeparamref name="TKey"/> Value property gives the key to order the given element by.</param>
  105. /// <param name="comparer">An <see cref="IComparer{TKey}"/> used to compare key values.</param>
  106. /// <returns>An <see cref="IObservableOrderedEnumerable{TSource}"/>, whose elements are sorted according to a key, or null when either <paramref name="source"/> or <paramref name="keySelector"/> is null.</returns>
  107. /// <remarks>The result of this method will only be reactive and observable if source is in fact an <see cref="IObservableOrderedEnumerable{TSource}"/>.</remarks>
  108. public static IOrderedEnumerable<TSource> ThenByDescending<TSource, TKey>(this IOrderedEnumerable<TSource> source, Func<TSource, IValueProvider<TKey>> keySelector, IComparer<TKey> comparer)
  109. {
  110. if (source == null || keySelector == null || comparer == null)
  111. return null;
  112. var asObservable = source as IObservableOrderedEnumerable<TSource>;
  113. return asObservable != null ? asObservable.CreateOrderedEnumerable(keySelector, comparer, true) : source.CreateOrderedEnumerable(FuncExtender<TSource>.Extend(keySelector, (i, ks) => ks(i).Value), comparer, true);
  114. }
  115. }
  116. }