Numeric string containing comparator

     String comparison normal numbers comprising more than a size determined number, mainly for some sort

   /// <Summary> 
        /// compare two strings, if the content with the number, according to the size of the digital numbers to compare.
        /// </ Summary> 
        /// <param name = "X"> </ param> 
        /// <param name = "Y"> </ param> 
        /// <Returns> </ Returns> 
        public  static  int StringCompareByNum ( String X, String Y) 
        { 
            // when there is agreement null, null empty judgment are equal, no empty large 
            IF (X == null || Y == null ) 
            { 
                IF (X == null && Y = = null ) 
                { 
                    return  0
                }
                else if (x != null)
                {
                    return 1;
                }
                else
                {
                    return -1;
                }
            }
            string strA = Convert.ToString(x);
            string strB = Convert.ToString(y);
            char[] arr1 = strA.ToCharArray();
            char[] arr2 = strB.ToCharArray();
            int i = 0, j = 0;
            while (i < arr1.Length && j < arr2.Length)
            {
                if (char.IsDigit(arr1[i]) && char.IsDigit(arr2[j]))
                {
                    string s1 = "", s2 = "";
                    while (i < arr1.Length && char.IsDigit(arr1[i]))
                    {
                        s1 += arr1[i];
                        i++;
                    }
                    while (j < arr2.Length && char.IsDigit(arr2[j]))
                    {
                        s2 += arr2[j];
                        j++;
                    }
                    if (double.Parse(s1) > double.Parse(s2))
                    {
                        return 1;
                    }
                    if (double.Parse(s1) < double.Parse(s2))
                    {
                        return -1;
                    }
                }
                else
                {
                    if (arr1[i] > arr2[j])
                    {
                        return 1;
                    }
                    if (arr1[i] < arr2[j])
                    {
                        return -1;
                    }
                    i++;
                    j++;
                }
            }
            if (arr1.Length == arr2.Length)
            {
                return 0;
            }
            else
            {
                return arr1.Length > arr2.Length ? 1 : -1;
            }
        }

 

Guess you like

Origin www.cnblogs.com/singlelcx/p/11203524.html