AlphaNumericStringComparer.cs 3.59 KB
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Windows.Forms;

namespace MoyaAdminLib
{
    public class AlphaNumericStringComparer : IComparer<string>
    {
        public int ColumnIndex;
        public bool SortOrderDesc;

        public AlphaNumericStringComparer()
        {
            ColumnIndex = 0;
        }

        public AlphaNumericStringComparer(int column)
        {
            ColumnIndex = 0;
        }

        public AlphaNumericStringComparer(int column, bool sortOrderDesc) 
		{
				this.SortOrderDesc = sortOrderDesc;
				ColumnIndex=column;
        }

        public int Compare(string x, string y)
        {
            if (x == null)
                x = "";
            if(y == null)
                y = "";

            decimal num1 = 0;
            decimal num2 = 0;

            if (Decimal.TryParse(x, out num1) && Decimal.TryParse(y, out num2))
            {
                return Compare(num1, num2);
            }

            int len1 = x.Length;
            int len2 = y.Length;
            int marker1 = 0;
            int marker2 = 0;

            // Walk through two the strings with two markers.
            while (marker1 < len1 && marker2 < len2)
            {
                char ch1 = x[marker1];
                char ch2 = y[marker2];

                // Some buffers we can build up characters in for each chunk.
                char[] space1 = new char[len1];
                int loc1 = 0;
                char[] space2 = new char[len2];
                int loc2 = 0;

                // Walk through all following characters that are digits or
                // characters in BOTH strings starting at the appropriate marker.
                // Collect char arrays.
                do
                {
                    space1[loc1++] = ch1;
                    marker1++;

                    if (marker1 < len1)
                    {
                        ch1 = x[marker1];
                    }
                    else
                    {
                        break;
                    }
                } while (char.IsDigit(ch1) == char.IsDigit(space1[0]));

                do
                {
                    space2[loc2++] = ch2;
                    marker2++;

                    if (marker2 < len2)
                    {
                        ch2 = y[marker2];
                    }
                    else
                    {
                        break;
                    }
                } while (char.IsDigit(ch2) == char.IsDigit(space2[0]));

                // If we have collected numbers, compare them numerically.
                // Otherwise, if we have strings, compare them alphabetically.
                string str1 = new string(space1);
                string str2 = new string(space2);

                int result;

                if (char.IsDigit(space1[0]) && char.IsDigit(space2[0]))
                {
                    int thisNumericChunk = int.Parse(str1);
                    int thatNumericChunk = int.Parse(str2);
                    result = thisNumericChunk.CompareTo(thatNumericChunk);
                }
                else
                {
                    result = str1.CompareTo(str2);
                }

                if (result != 0)
                {
                    return result;
                }
            }
            return len1 - len2;
        }

        public int Compare(decimal x, decimal y)
        {
            if (x > y)
                return 1;
            else if (x < y)
                return -1;
            else
                return 0;
        }

    }
}