Custom Sort: Define your own sorting sequence

using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
    class CustomSort
    {
        static void Main(string[] args)
        {
            //Create the reference array for custom sort
            string[] refArray = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
                                  "a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
                                  "k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
                                  "u", "v", "w", "x", "y", "z",
                                  "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
                                  "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
                                  "U", "V", "W", "X", "Y", "Z"
                                };
            string[] initialStr = { "aaa", "aaA", "aAa", "Aaa", "AAa", "AAA", "AaA", "aAA"};
            Console.WriteLine("Initial String Order: ");
            foreach (String str in initialStr)
            {
                Console.WriteLine(str);
            }
            Console.WriteLine("Sorted String Order: ");
            for (int i = 0; i < initialStr.Length – 1; i++)
            {
                for (int j = i + 1; j < initialStr.Length; j++)
                {
                    if (Compare(refArray, initialStr[i], initialStr[j]) < 0)
                    {
                        string temp = initialStr[i];
                        initialStr[i] = initialStr[j];
                        initialStr[j] = temp;
                    }
                }
            }
            foreach (String str in initialStr)
            {
                Console.WriteLine(str);
            }
        }
        //Static method to compare the strings
        static int Compare(string[] refArray, string a, string b)
        {
            int compareLength = 0;
            if (a.Length > b.Length)
            {
                compareLength = b.Length;
            }
            else if (a.Length < b.Length)
            {
                compareLength = a.Length;
            }
            else
            {
                compareLength = a.Length;
            }
            for (int i = 0; i < compareLength; i++)
            {
                //Add comparison logic here
                //return 0 if a < b
                if(GetIndex(refArray, a[i].ToString()) < GetIndex(refArray, b[i].ToString()))
                {
                    return 1;
                }
                //return 1 if a > b
                else if (GetIndex(refArray, a[i].ToString()) > GetIndex(refArray, b[i].ToString()))
                {
                    return -1;
                }
            }
            //return 0 if a = b
            if (a.Length == b.Length)
            {
                return 0;
            }
            //return 1 if compared strings are equal but length of a < length of b
            else if (a.Length < b.Length)
            {
                return 1;
            }
            //return -1 if compared strings are equal but length of a < length of b
            else
            {
                return -1;
            }
        }
        //Static method to get the string index
        static int GetIndex(string[] refArray, string str)
        {
            int i = 0;
            for (i = 0; i < refArray.Length; i++)
            {
                if (str == refArray[i])
                {
                    break;
                }
            }
            return i;
        }
    }
}
Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s