NaturalStringComparer.cs 2.8 KB

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