Wednesday, March 2, 2016

Alphanumeric sorting using C#




using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AlphanumericSorter
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] highways = new string[]
            {
                "P-1",
                "P-2",
                "P-10",
                "P-11"
            };
            //
            // We want to sort a string array called highways in an
            // alphanumeric way. Call the static Array.Sort method.
            //
            Array.Sort(highways, new AlphanumComparatorFast());
            //
            // Display the results
            //
            foreach (string h in highways)
            {
                Console.WriteLine(h);
            }
            Console.Read();
        }
    }

    public class AlphanumComparatorFast : IComparer
    {
        public int Compare(object x, object y)
        {
            string s1 = x as string;
            if (s1 == null)
            {
                return 0;
            }
            string s2 = y as string;
            if (s2 == null)
            {
                return 0;
            }

            int len1 = s1.Length;
            int len2 = s2.Length;
            int marker1 = 0;
            int marker2 = 0;

            // Walk through two the strings with two markers.
            while (marker1 < len1 && marker2 < len2)
            {
                char ch1 = s1[marker1];
                char ch2 = s2[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 = s1[marker1];
                    }
                    else
                    {
                        break;
                    }
                } while (char.IsDigit(ch1) == char.IsDigit(space1[0]));

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

                    if (marker2 < len2)
                    {
                        ch2 = s2[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;
        }
    }
}

2 comments:

  1. your code is shit it doesnt work when I copy and paste it into notepad

    ReplyDelete
  2. I'm really enjoying the template/theme of this site. It's simple, yet effective. A lot of times it's hard to get that "perfect balance" between user friendliness and visual appeal. I must say you've done a superb job with this. Additionally, the blog loads very fast for me on Chrome. Exceptional Blog! Check out this site: How To Password Protect Folder The Marine Way.

    ReplyDelete