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.

165 lines
5.2 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace Apewer.Internals
  5. {
  6. internal class SortHelper
  7. {
  8. #region List<String>
  9. public static List<string> ListString(List<string> argList, bool argAscend)
  10. {
  11. var valid = new List<string>();
  12. var origin = argList;
  13. if (origin == null) return valid;
  14. if (origin.Count < 2)
  15. {
  16. if (origin.Count == 1) valid.Add(origin[0]);
  17. return valid;
  18. }
  19. foreach (var item in origin)
  20. {
  21. if (string.IsNullOrEmpty(item)) continue;
  22. var trim = item.Trim();
  23. if (string.IsNullOrEmpty(trim)) continue;
  24. valid.Add(trim);
  25. }
  26. if (argAscend) valid.Sort(new Comparison<string>(ComparisonAscend));
  27. else valid.Sort(new Comparison<string>(ComparisonDescend));
  28. return valid;
  29. }
  30. public static int ComparisonAscend(string x, string y)
  31. {
  32. return x.CompareTo(y);
  33. }
  34. public static int ComparisonDescend(string x, string y)
  35. {
  36. return y.CompareTo(x);
  37. }
  38. #endregion
  39. #region Dictionary<String, String>
  40. public static Dictionary<string, string> DictionaryStringString(Dictionary<string, string> argDictionary, bool argKey, bool argAscend)
  41. {
  42. var input = argDictionary;
  43. var list = new List<KeyValuePair<string, string>>(input);
  44. if (list.Count > 0)
  45. {
  46. try
  47. {
  48. if (argKey)
  49. {
  50. if (argAscend) list.Sort(new Comparison<KeyValuePair<string, string>>(ComparisonAscendKey));
  51. else list.Sort(new Comparison<KeyValuePair<string, string>>(ComparisonDescendKey));
  52. }
  53. else
  54. {
  55. if (argAscend) list.Sort(new Comparison<KeyValuePair<string, string>>(ComparisonAscendValue));
  56. else list.Sort(new Comparison<KeyValuePair<string, string>>(ComparisonDescendValue));
  57. }
  58. }
  59. catch
  60. {
  61. list.Clear();
  62. }
  63. }
  64. var output = new Dictionary<string, string>();
  65. foreach (var kvp in list)
  66. {
  67. output.Add(kvp.Key, kvp.Value);
  68. }
  69. return output;
  70. }
  71. public static int ComparisonAscendKey(KeyValuePair<string, string> x, KeyValuePair<string, string> y)
  72. {
  73. return x.Key.CompareTo(y.Key);
  74. }
  75. public static int ComparisonDescendKey(KeyValuePair<string, string> x, KeyValuePair<string, string> y)
  76. {
  77. return y.Key.CompareTo(x.Key);
  78. }
  79. public static int ComparisonAscendValue(KeyValuePair<string, string> x, KeyValuePair<string, string> y)
  80. {
  81. return x.Value.CompareTo(y.Value);
  82. }
  83. public static int ComparisonDescendValue(KeyValuePair<string, string> x, KeyValuePair<string, string> y)
  84. {
  85. return y.Value.CompareTo(x.Value);
  86. }
  87. #endregion
  88. #region Dictionary<String, Double>
  89. public static Dictionary<string, double> DictionaryStringDouble(Dictionary<string, double> argDictionary, bool argKey, bool argAscend)
  90. {
  91. var input = argDictionary;
  92. var list = new List<KeyValuePair<string, double>>(input);
  93. if (list.Count > 0)
  94. {
  95. try
  96. {
  97. if (argKey)
  98. {
  99. if (argAscend) list.Sort(new Comparison<KeyValuePair<string, double>>(ComparisonAscendKey));
  100. else list.Sort(new Comparison<KeyValuePair<string, double>>(ComparisonDescendKey));
  101. }
  102. else
  103. {
  104. if (argAscend) list.Sort(new Comparison<KeyValuePair<string, double>>(ComparisonAscendValue));
  105. else list.Sort(new Comparison<KeyValuePair<string, double>>(ComparisonDescendValue));
  106. }
  107. }
  108. catch
  109. {
  110. list.Clear();
  111. }
  112. }
  113. var output = new Dictionary<string, double>();
  114. foreach (var kvp in list)
  115. {
  116. output.Add(kvp.Key, kvp.Value);
  117. }
  118. return output;
  119. }
  120. public static int ComparisonAscendKey(KeyValuePair<string, double> x, KeyValuePair<string, double> y)
  121. {
  122. return x.Key.CompareTo(y.Key);
  123. }
  124. public static int ComparisonDescendKey(KeyValuePair<string, double> x, KeyValuePair<string, double> y)
  125. {
  126. return y.Key.CompareTo(x.Key);
  127. }
  128. public static int ComparisonAscendValue(KeyValuePair<string, double> x, KeyValuePair<string, double> y)
  129. {
  130. return x.Value.CompareTo(y.Value);
  131. }
  132. public static int ComparisonDescendValue(KeyValuePair<string, double> x, KeyValuePair<string, double> y)
  133. {
  134. return y.Value.CompareTo(x.Value);
  135. }
  136. #endregion
  137. }
  138. }