NaturalStringComparer.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System.Collections.Generic;
  2. namespace Wayne.Lib
  3. {
  4. /// <summary>
  5. /// Comparer used to sort strings naturally (i.e. "5" comes before "10" and "x5" comes before "x10").
  6. /// </summary>
  7. internal class NaturalStringComparer : IComparer<string>
  8. {
  9. /// <summary>
  10. /// Compares two objects and returns a value indicating whether one is less than, equal to, or greater than the other.
  11. /// </summary>
  12. /// <param name="x"></param>
  13. /// <param name="y"></param>
  14. /// <returns></returns>
  15. public int Compare(string x, string y)
  16. {
  17. int len1 = x.Length;
  18. int len2 = y.Length;
  19. int marker1 = 0;
  20. int marker2 = 0;
  21. // Walk through two the strings with two markers.
  22. while (marker1 < len1 && marker2 < len2)
  23. {
  24. char ch1 = x[marker1];
  25. char ch2 = y[marker2];
  26. // Some buffers we can build up characters in for each chunk.
  27. char[] space1 = new char[len1];
  28. int loc1 = 0;
  29. char[] space2 = new char[len2];
  30. int loc2 = 0;
  31. // Walk through all following characters that are digits or
  32. // characters in BOTH strings starting at the appropriate marker.
  33. // Collect char arrays.
  34. do
  35. {
  36. space1[loc1++] = ch1;
  37. marker1++;
  38. if (marker1 < len1)
  39. ch1 = x[marker1];
  40. else
  41. break;
  42. }
  43. while (char.IsDigit(ch1) == char.IsDigit(space1[0]));
  44. do
  45. {
  46. space2[loc2++] = ch2;
  47. marker2++;
  48. if (marker2 < len2)
  49. ch2 = y[marker2];
  50. else
  51. break;
  52. }
  53. while (char.IsDigit(ch2) == char.IsDigit(space2[0]));
  54. // If we have collected numbers, compare them numerically.
  55. // Otherwise, if we have strings, compare them alphabetically.
  56. string str1 = new string(space1);
  57. string str2 = new string(space2);
  58. int result;
  59. if (char.IsDigit(space1[0]) && char.IsDigit(space2[0]))
  60. {
  61. int thisNumericChunk = int.Parse(str1);
  62. int thatNumericChunk = int.Parse(str2);
  63. result = thisNumericChunk.CompareTo(thatNumericChunk);
  64. }
  65. else
  66. result = str1.CompareTo(str2);
  67. if (result != 0)
  68. return result;
  69. }
  70. return len1 - len2;
  71. }
  72. }
  73. }