Click here to Skip to main content
15,881,712 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
var strTest = new List<string> { "B1", "B2","b3","0", "B3", "B10","B20","B90","GB2","B9","B21","GB1","GB11" };

I need to sort the above list as
"0, b3,B1, B2, B3, B9, B10, B20, B21, B90, GB1, GB2,GB11"

Can anyone help me with this one.

Thanks in Advance,
Posted
Updated 8-Oct-14 0:21am
v2

Take a look at these CodeProject articles:
Natural Sort Comparer[^]
Sorting Strings for Humans with IComparer[^]
 
Share this answer
 
v2
Comments
[no name] 8-Oct-14 6:36am    
In the second link you will also find a interesting comment:
http://www.codeproject.com/Messages/2359695/Using-API-method-for-logical-string-comparison.aspx
Thomas Daniels 8-Oct-14 6:52am    
Interesting, thanks for sharing!
Manu Prasad 8-Oct-14 6:49am    
Thanks ProgramFOX....
Thomas Daniels 8-Oct-14 6:52am    
You're welcome!
You might write you own comparer (see MSDN: "List<t>.Sort Method (Comparison<t>)"[^]. In its implementation you have to first split the strings into their alphabetic and numeric parts (handling the corner cases, like "0") and then evaluate the respective order based first on the alphabetic and then on the numeric parts.
 
Share this answer
 
Thanks Everyone...
Its working nicely..
C#
static void Main()
{
    string[] highways = new string[] { "B1", "B2", "b3", "0", "B3", "B10", "B20", "B90", "B9", "B21", "GB1", "GB11" };
    Array.Sort(highways, new AlphanumComparatorFast());

    foreach (string item in highways)
    {
        Console.WriteLine(item);
    }

    Console.ReadLine();
}


C#
public class AlphanumComparatorFast : System.Collections.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;

            while (marker1 < len1 && marker2 < len2)
            {
                char ch1 = s1[marker1];
                char ch2 = s2[marker2];

                char[] space1 = new char[len1];
                int loc1 = 0;
                char[] space2 = new char[len2];
                int loc2 = 0;

                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]));

                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;
        }
    }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900