oliver
oliver

Reputation: 2891

Case sensitive sorting with capitals last

I want to sort a List< string> case sensitive with capitals last, i.e. in the order

a,aBC,b,bXY,c,A,Ax,B,C,...

I have tried

Comparison<string> c = (string x1,string x2) => StringComparer.Ordinal.Compare(x1,x2);
list.Sort(c);

but it returns

A,Ax,B,C,a,aBC,b,bXY,c,...

Is there any predefined method to do this, or will I need to fiddle something together myself?

Edit: Since "capitals last" seems difficult enough I have spared numbers for the moment.

Upvotes: 7

Views: 627

Answers (3)

oliver
oliver

Reputation: 2891

Finally I have opted for the suggestion by @JohnTortugo. Thanks John, incredibly simple idea, short and easy to implement!

  1. Swap upper and lower case in a string by XOR'ing 0x20 with each byte (ASCII coding assumed):

    static string swapUpperLower(string s)
    {
        return System.Text.Encoding.ASCII.GetString(System.Text.Encoding.ASCII.GetBytes(s).Select(c => (byte)(c^0x20)).ToArray());
    }
    
  2. Swap case for every string in the list before and after sorting

        Comparison<string> c = (string x1,string x2) => StringComparer.Ordinal.Compare(x1,x2);
    
        sl = sl.Select(s => swapUpperLower(s)).ToList();
        sl.Sort(c);
        sl = sl.Select(s => swapUpperLower(s)).ToList();
    

Result:

a aBC b bXY c A Ax B C

Upvotes: 2

Agent_Orange
Agent_Orange

Reputation: 351

You can try this lengthy code It works Like A Charm

public static List<String> CustomSort(List<String> ls)
    {

        ls.Sort();
        List<String> Oint = new List<String>();
        List<String> Ocap = new List<String>();
        List<String> Osma = new List<String>();

        foreach(string s in ls)
        {
            int n;
            bool isNumeric = int.TryParse(s, out n);
            if(isNumeric)
            {
                Oint.Add(s);
            }
            else if (char.IsUpper(s[0]))
            {
                Ocap.Add(s);
            }
            else if (!char.IsUpper(s[0]))
            {
                Osma.Add(s);
            }
        }

        var r1 = Oint.Concat(Osma).Concat(Ocap);

        List<String> com = r1.ToList();
        return com;

    }

Then Call it Like

List<String> ls = new List<String>();
            ls.Add("1");
            ls.Add("Cdsd");
            ls.Add("BCCd");
            ls.Add("ADDD");
            ls.Add("3");
            ls.Add("2");
            ls.Add("aDFD");
            ls.Add("cSS");
            ls.Add("b");
        List<String> cuz = CustomSort(ls);
        foreach(String s in cuz)
        {
            Console.WriteLine(s);
        }

You can Try This Code here

Upvotes: 0

rahulaga-msft
rahulaga-msft

Reputation: 4164

try this :

byte[] ascii = Encoding.ASCII.GetBytes("aBcDEf");
        var result = ascii.OrderByDescending(b => b).ToList();
        string converted = string.Empty;
        foreach(var c in result)
        {
            converted += Char.ConvertFromUtf32(c);
        }
        Console.WriteLine(converted);

enter image description here

Upvotes: 0

Related Questions