SortExtensions.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace MediaBrowser.Controller.Sorting
  6. {
  7. public static class SortExtensions
  8. {
  9. public static IEnumerable<T> OrderByString<T>(this IEnumerable<T> list, Func<T, string> getName)
  10. {
  11. return list.OrderBy(getName, new AlphanumComparator());
  12. }
  13. public static IEnumerable<T> OrderByStringDescending<T>(this IEnumerable<T> list, Func<T, string> getName)
  14. {
  15. return list.OrderByDescending(getName, new AlphanumComparator());
  16. }
  17. public static IOrderedEnumerable<T> ThenByString<T>(this IOrderedEnumerable<T> list, Func<T, string> getName)
  18. {
  19. return list.ThenBy(getName, new AlphanumComparator());
  20. }
  21. public static IOrderedEnumerable<T> ThenByStringDescending<T>(this IOrderedEnumerable<T> list, Func<T, string> getName)
  22. {
  23. return list.ThenByDescending(getName, new AlphanumComparator());
  24. }
  25. private class AlphanumComparator : IComparer<string>
  26. {
  27. private enum ChunkType { Alphanumeric, Numeric };
  28. private static bool InChunk(char ch, char otherCh)
  29. {
  30. var type = ChunkType.Alphanumeric;
  31. if (char.IsDigit(otherCh))
  32. {
  33. type = ChunkType.Numeric;
  34. }
  35. if ((type == ChunkType.Alphanumeric && char.IsDigit(ch))
  36. || (type == ChunkType.Numeric && !char.IsDigit(ch)))
  37. {
  38. return false;
  39. }
  40. return true;
  41. }
  42. public static int CompareValues(string s1, string s2)
  43. {
  44. if (s1 == null || s2 == null)
  45. {
  46. return 0;
  47. }
  48. int thisMarker = 0, thisNumericChunk = 0;
  49. int thatMarker = 0, thatNumericChunk = 0;
  50. while ((thisMarker < s1.Length) || (thatMarker < s2.Length))
  51. {
  52. if (thisMarker >= s1.Length)
  53. {
  54. return -1;
  55. }
  56. else if (thatMarker >= s2.Length)
  57. {
  58. return 1;
  59. }
  60. char thisCh = s1[thisMarker];
  61. char thatCh = s2[thatMarker];
  62. var thisChunk = new StringBuilder();
  63. var thatChunk = new StringBuilder();
  64. while ((thisMarker < s1.Length) && (thisChunk.Length == 0 || InChunk(thisCh, thisChunk[0])))
  65. {
  66. thisChunk.Append(thisCh);
  67. thisMarker++;
  68. if (thisMarker < s1.Length)
  69. {
  70. thisCh = s1[thisMarker];
  71. }
  72. }
  73. while ((thatMarker < s2.Length) && (thatChunk.Length == 0 || InChunk(thatCh, thatChunk[0])))
  74. {
  75. thatChunk.Append(thatCh);
  76. thatMarker++;
  77. if (thatMarker < s2.Length)
  78. {
  79. thatCh = s2[thatMarker];
  80. }
  81. }
  82. int result = 0;
  83. // If both chunks contain numeric characters, sort them numerically
  84. if (char.IsDigit(thisChunk[0]) && char.IsDigit(thatChunk[0]))
  85. {
  86. if (!int.TryParse(thisChunk.ToString(), out thisNumericChunk))
  87. {
  88. return 0;
  89. }
  90. if (!int.TryParse(thatChunk.ToString(), out thatNumericChunk))
  91. {
  92. return 0;
  93. }
  94. if (thisNumericChunk < thatNumericChunk)
  95. {
  96. result = -1;
  97. }
  98. if (thisNumericChunk > thatNumericChunk)
  99. {
  100. result = 1;
  101. }
  102. }
  103. else
  104. {
  105. result = thisChunk.ToString().CompareTo(thatChunk.ToString());
  106. }
  107. if (result != 0)
  108. {
  109. return result;
  110. }
  111. }
  112. return 0;
  113. }
  114. public int Compare(string x, string y)
  115. {
  116. return CompareValues(x, y);
  117. }
  118. }
  119. }
  120. }