OdraEncoded

Series string comparer

Oct 17th, 2012
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.34 KB | None | 0 0
  1. public int Compare (string x, string y)
  2. {
  3.     if (x == y)
  4.         return 0;
  5.  
  6.     int xi = 0;
  7.     int yi = 0;
  8.  
  9.     while (xi < x.Length && yi < y.Length) {
  10.         while(xi < x.Length && char.IsWhiteSpace(x[xi])) xi++;
  11.         while(yi < y.Length && char.IsWhiteSpace(y[yi])) yi++;
  12.  
  13.         if(char.IsDigit(x[xi]) && char.IsDigit(y[yi]))
  14.         {
  15.             string xDigits = "";
  16.  
  17.             do
  18.             {
  19.                 xDigits += x[xi];
  20.                 xi++;
  21.             }
  22.             while(xi < x.Length && char.IsDigit(x[xi]));
  23.  
  24.             string yDigits = "";
  25.  
  26.             do
  27.             {
  28.                 yDigits += y[yi];
  29.                 yi++;
  30.             }
  31.             while(yi < y.Length && char.IsDigit(y[yi]));
  32.  
  33.             int xVal = ParseDigits(xDigits);
  34.             int yVal = ParseDigits(yDigits);
  35.  
  36.             if(xVal > yVal) return 1;
  37.             else if (xVal < yVal) return -1;
  38.         }
  39.         else if(x[xi] != y[yi])
  40.         {
  41.             return x[xi].CompareTo(y[yi]);
  42.         }
  43.         else
  44.         {
  45.             xi++;
  46.             yi++;
  47.         }
  48.     }
  49.  
  50.     return 0;
  51. }
  52.  
  53. public static int ParseDigits (string digits)
  54. {
  55.     string newDigits = "";
  56.  
  57.     foreach (char c in digits) {
  58.         if (c >= '0' && c <= '9')
  59.             newDigits += c;
  60.         else if (c >= '\uFF10' && c <= '\uFF19')
  61.             newDigits += (char)(c - '\uFF10' + '0');
  62.         else if (c >= '\u0660' && c <= '\u0669')
  63.             newDigits += (char)(c - '\u0660' + '0');
  64.         else if (c >= '\u09E6' && c <= '\u09EF')
  65.             newDigits += (char)(c - '\u09E6' + '0');
  66.     }
  67.  
  68.     if (newDigits == "")
  69.         return 0;
  70.     else
  71.         return int.Parse (newDigits);
  72. }
Advertisement
Add Comment
Please, Sign In to add comment