You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
5.4 KiB

  1. using Apewer.Internals;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. namespace Apewer
  6. {
  7. /// <summary>排序实用工具。</summary>
  8. public class SortUtility
  9. {
  10. #region compare
  11. /// <exception cref="ArgumentException"></exception>
  12. public static int Ascend<T>(T a, T b) where T : IComparable
  13. {
  14. if (a == null) return b == null ? 0 : b.CompareTo(a);
  15. return b == null ? 1 : a.CompareTo(b);
  16. }
  17. /// <exception cref="ArgumentException"></exception>
  18. public static int Descend<T>(T a, T b) where T : IComparable
  19. {
  20. return 0 - Ascend(a, b);
  21. }
  22. /// <exception cref="ArgumentException"></exception>
  23. public static int AscendT<T>(T a, T b) where T : IComparable<T>
  24. {
  25. if (a == null) return b == null ? 0 : b.CompareTo(a);
  26. return b == null ? 1 : a.CompareTo(b);
  27. }
  28. /// <exception cref="ArgumentException"></exception>
  29. public static int DescendT<T>(T a, T b) where T : IComparable<T>
  30. {
  31. return 0 - AscendT(a, b);
  32. }
  33. #endregion
  34. #region enumerable
  35. /// <summary>升序排序。</summary>
  36. public static List<T> Ascend<T>(IEnumerable<T> items) where T : IComparable
  37. {
  38. var list = new List<T>(items);
  39. list.Sort(Ascend);
  40. return list;
  41. }
  42. /// <summary>降序排序。</summary>
  43. public static List<T> Descend<T>(IEnumerable<T> items) where T : IComparable
  44. {
  45. var list = new List<T>(items);
  46. list.Sort(Descend);
  47. return list;
  48. }
  49. /// <summary>升序排序。</summary>
  50. public static List<T> AscendT<T>(IEnumerable<T> items) where T : IComparable<T>
  51. {
  52. var list = new List<T>(items);
  53. list.Sort(AscendT);
  54. return list;
  55. }
  56. /// <summary>降序排序。</summary>
  57. public static List<T> DescendT<T>(IEnumerable<T> items) where T : IComparable<T>
  58. {
  59. var list = new List<T>(items);
  60. list.Sort(DescendT);
  61. return list;
  62. }
  63. #endregion
  64. #region dictionary
  65. /// <summary>对 Key 排序。</summary>
  66. public static List<KeyValuePair<TKey, TValue>> Keys<TKey, TValue>(Dictionary<TKey, TValue> target, Comparison<TKey> comparison) where TKey : IComparable<TKey>
  67. {
  68. if (target == null) return null;
  69. var keys = new List<TKey>();
  70. keys.AddRange(target.Keys);
  71. if (comparison == null) keys.Sort();
  72. else keys.Sort(comparison);
  73. var output = new List<KeyValuePair<TKey, TValue>>();
  74. foreach (var key in keys) output.Add(key, target[key]);
  75. return output;
  76. }
  77. /// <summary>对 Key 升序排序。</summary>
  78. public static List<KeyValuePair<TKey, TValue>> KeysAscend<TKey, TValue>(Dictionary<TKey, TValue> target) where TKey : IComparable<TKey>
  79. {
  80. return Keys(target, new Comparison<TKey>(AscendT));
  81. }
  82. /// <summary>对 Key 升序排序。</summary>
  83. public static List<KeyValuePair<TKey, TValue>> KeysDescend<TKey, TValue>(Dictionary<TKey, TValue> target) where TKey : IComparable<TKey>
  84. {
  85. return Keys(target, new Comparison<TKey>(DescendT));
  86. }
  87. /// <summary>对 Key 升序排序。</summary>
  88. public static Dictionary<string, string> SortKeyAscend(Dictionary<string, string> dictionary)
  89. {
  90. return SortHelper.DictionaryStringString(dictionary, true, true);
  91. }
  92. /// <summary>对 Key 降序排序。</summary>
  93. public static Dictionary<string, string> SortKeyDecend(Dictionary<string, string> dictionary)
  94. {
  95. return SortHelper.DictionaryStringString(dictionary, true, false);
  96. }
  97. /// <summary>对 Value 升序排序。</summary>
  98. public static Dictionary<string, string> SortValueAscend(Dictionary<string, string> dictionary)
  99. {
  100. return SortHelper.DictionaryStringString(dictionary, false, true);
  101. }
  102. /// <summary>对 Value 降序排序。</summary>
  103. public static Dictionary<string, string> SortValueDecend(Dictionary<string, string> dictionary)
  104. {
  105. return SortHelper.DictionaryStringString(dictionary, false, false);
  106. }
  107. /// <summary>对 Key 升序排序。</summary>
  108. public static Dictionary<string, double> SortKeyAscend(Dictionary<string, double> dictionary)
  109. {
  110. return SortHelper.DictionaryStringDouble(dictionary, true, true);
  111. }
  112. /// <summary>对 Key 降序排序。</summary>
  113. public static Dictionary<string, double> SortKeyDecend(Dictionary<string, double> dictionary)
  114. {
  115. return SortHelper.DictionaryStringDouble(dictionary, true, false);
  116. }
  117. /// <summary>对 Value 升序排序。</summary>
  118. public static Dictionary<string, double> SortValueAscend(Dictionary<string, double> dictionary)
  119. {
  120. return SortHelper.DictionaryStringDouble(dictionary, false, true);
  121. }
  122. /// <summary>对 Value 降序排序。</summary>
  123. public static Dictionary<string, double> SortValueDecend(Dictionary<string, double> dictionary)
  124. {
  125. return SortHelper.DictionaryStringDouble(dictionary, false, false);
  126. }
  127. #endregion
  128. }
  129. }