BaseItemComparer.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. using MediaBrowser.Common.Logging;
  2. using MediaBrowser.Controller.Entities;
  3. using System;
  4. using System.Collections.Generic;
  5. namespace MediaBrowser.Controller.Sorting {
  6. /// <summary>
  7. /// Class BaseItemComparer
  8. /// </summary>
  9. public class BaseItemComparer : IComparer<BaseItem> {
  10. /// <summary>
  11. /// The _order
  12. /// </summary>
  13. private readonly SortOrder _order;
  14. /// <summary>
  15. /// The _property name
  16. /// </summary>
  17. private readonly string _propertyName;
  18. /// <summary>
  19. /// The _compare culture
  20. /// </summary>
  21. private readonly StringComparison _compareCulture = StringComparison.CurrentCultureIgnoreCase;
  22. /// <summary>
  23. /// Initializes a new instance of the <see cref="BaseItemComparer" /> class.
  24. /// </summary>
  25. /// <param name="order">The order.</param>
  26. public BaseItemComparer(SortOrder order) {
  27. _order = order;
  28. }
  29. /// <summary>
  30. /// Initializes a new instance of the <see cref="BaseItemComparer" /> class.
  31. /// </summary>
  32. /// <param name="order">The order.</param>
  33. /// <param name="compare">The compare.</param>
  34. public BaseItemComparer(SortOrder order, StringComparison compare) {
  35. _order = order;
  36. _compareCulture = compare;
  37. }
  38. /// <summary>
  39. /// Initializes a new instance of the <see cref="BaseItemComparer" /> class.
  40. /// </summary>
  41. /// <param name="property">The property.</param>
  42. public BaseItemComparer(string property) {
  43. _order = SortOrder.Custom;
  44. _propertyName = property;
  45. }
  46. /// <summary>
  47. /// Initializes a new instance of the <see cref="BaseItemComparer" /> class.
  48. /// </summary>
  49. /// <param name="property">The property.</param>
  50. /// <param name="compare">The compare.</param>
  51. public BaseItemComparer(string property, StringComparison compare) {
  52. _order = SortOrder.Custom;
  53. _propertyName = property;
  54. _compareCulture = compare;
  55. }
  56. #region IComparer<BaseItem> Members
  57. /// <summary>
  58. /// Compares the specified x.
  59. /// </summary>
  60. /// <param name="x">The x.</param>
  61. /// <param name="y">The y.</param>
  62. /// <returns>System.Int32.</returns>
  63. public int Compare(BaseItem x, BaseItem y) {
  64. int compare = 0;
  65. switch (_order) {
  66. case SortOrder.Date:
  67. compare = -x.DateCreated.CompareTo(y.DateCreated);
  68. break;
  69. case SortOrder.Year:
  70. var xProductionYear = x.ProductionYear ?? 0;
  71. var yProductionYear = y.ProductionYear ?? 0;
  72. compare = yProductionYear.CompareTo(xProductionYear);
  73. break;
  74. case SortOrder.Rating:
  75. var xRating = x.CommunityRating ?? 0;
  76. var yRating = y.CommunityRating ?? 0;
  77. compare = yRating.CompareTo(xRating);
  78. break;
  79. case SortOrder.Runtime:
  80. var xRuntime = x.RunTimeTicks ?? 0;
  81. var yRuntime = y.RunTimeTicks ?? 0;
  82. compare = xRuntime.CompareTo(yRuntime);
  83. break;
  84. case SortOrder.Custom:
  85. Logger.LogDebugInfo("Sorting on custom field " + _propertyName);
  86. var yProp = y.GetType().GetProperty(_propertyName);
  87. var xProp = x.GetType().GetProperty(_propertyName);
  88. if (yProp == null || xProp == null) break;
  89. var yVal = yProp.GetValue(y, null);
  90. var xVal = xProp.GetValue(x,null);
  91. if (yVal == null && xVal == null) break;
  92. if (yVal == null) return 1;
  93. if (xVal == null) return -1;
  94. compare = String.Compare(xVal.ToString(), yVal.ToString(),_compareCulture);
  95. break;
  96. default:
  97. compare = 0;
  98. break;
  99. }
  100. if (compare == 0) {
  101. var name1 = x.SortName ?? x.Name ?? "";
  102. var name2 = y.SortName ?? y.Name ?? "";
  103. //if (Config.Instance.EnableAlphanumericSorting)
  104. compare = AlphaNumericCompare(name1, name2,_compareCulture);
  105. //else
  106. // compare = String.Compare(name1,name2,_compareCulture);
  107. }
  108. return compare;
  109. }
  110. #endregion
  111. /// <summary>
  112. /// Alphas the numeric compare.
  113. /// </summary>
  114. /// <param name="s1">The s1.</param>
  115. /// <param name="s2">The s2.</param>
  116. /// <param name="compareCulture">The compare culture.</param>
  117. /// <returns>System.Int32.</returns>
  118. public static int AlphaNumericCompare(string s1, string s2, StringComparison compareCulture) {
  119. // http://dotnetperls.com/Content/Alphanumeric-Sorting.aspx
  120. int len1 = s1.Length;
  121. int len2 = s2.Length;
  122. int marker1 = 0;
  123. int marker2 = 0;
  124. // Walk through two the strings with two markers.
  125. while (marker1 < len1 && marker2 < len2) {
  126. char ch1 = s1[marker1];
  127. char ch2 = s2[marker2];
  128. // Some buffers we can build up characters in for each chunk.
  129. var space1 = new char[len1];
  130. var loc1 = 0;
  131. var space2 = new char[len2];
  132. var loc2 = 0;
  133. // Walk through all following characters that are digits or
  134. // characters in BOTH strings starting at the appropriate marker.
  135. // Collect char arrays.
  136. do {
  137. space1[loc1++] = ch1;
  138. marker1++;
  139. if (marker1 < len1) {
  140. ch1 = s1[marker1];
  141. } else {
  142. break;
  143. }
  144. } while (char.IsDigit(ch1) == char.IsDigit(space1[0]));
  145. do {
  146. space2[loc2++] = ch2;
  147. marker2++;
  148. if (marker2 < len2) {
  149. ch2 = s2[marker2];
  150. } else {
  151. break;
  152. }
  153. } while (char.IsDigit(ch2) == char.IsDigit(space2[0]));
  154. // If we have collected numbers, compare them numerically.
  155. // Otherwise, if we have strings, compare them alphabetically.
  156. var str1 = new string(space1);
  157. var str2 = new string(space2);
  158. var result = 0;
  159. //biggest int - 2147483647
  160. if (char.IsDigit(space1[0]) && char.IsDigit(space2[0]) /*&& str1.Length < 10 && str2.Length < 10*/) //this assumed the entire string was a number...
  161. {
  162. int thisNumericChunk;
  163. var isValid = false;
  164. if (int.TryParse(str1.Substring(0, str1.Length > 9 ? 10 : str1.Length), out thisNumericChunk))
  165. {
  166. int thatNumericChunk;
  167. if (int.TryParse(str2.Substring(0, str2.Length > 9 ? 10 : str2.Length), out thatNumericChunk))
  168. {
  169. isValid = true;
  170. result = thisNumericChunk.CompareTo(thatNumericChunk);
  171. }
  172. }
  173. if (!isValid)
  174. {
  175. Logger.LogError("Error comparing numeric strings: " + str1 + "/" + str2);
  176. result = String.Compare(str1, str2, compareCulture);
  177. }
  178. } else {
  179. result = String.Compare(str1,str2,compareCulture);
  180. }
  181. if (result != 0) {
  182. return result;
  183. }
  184. }
  185. return len1 - len2;
  186. }
  187. }
  188. }