Sixteen
Sixteen

Reputation: 563

How to sort on 2 distinct rules in List<string>

I need to sort a List<string> following 2 rules.

My string element will always be formatted as XXXnnnnE or XXXnnnnD where X are capitalized letters from A to Z and n are digit from 0 to 9.

I want to sort my list alphabetically, but I want E string to come before D string as shown below

DFG0001D
AWK0007E
ORK0127E
AWK0007D
DFG0001E
ORK0127D

need to be sorted as

AWK0007E
AWK0007D
DFG0001E
DFG0001D
ORK0127E
ORK0127D

How could I achieve this ?

Thanks for help

Upvotes: 1

Views: 423

Answers (4)

you can use a custom delegate and compare the 1st 3 chars and the last one:

List<string> x = new List<string>();
x.Add("DFG0001D");
x.Add("AWK0007E");
x.Add("ORK0127E");
x.Add("AWK0007D");
x.Add("DFG0001E");
x.Add("ORK0127D");

x.Sort(delegate(string c1, string c2) { 
string a = c1.Substring(0, 3)+c1.Substring(c1.Length-1, 1);
string b = c2.Substring(0, 3)+c2.Substring(c2.Length-1, 1);
return (a.CompareTo(b)); 

});

Console.WriteLine("After sort...");
foreach (string i in x)
{
    Console.WriteLine(i);
}

Fiddle example : https://dotnetfiddle.net/YAzvB4

Upvotes: 1

KUBA
KUBA

Reputation: 36

just implement your own comparer like this:

class CustomStringComparer : IComparer<string>
{
    private readonly IComparer<string> _baseComparer;
    public CustomStringComparer(IComparer<string> baseComparer)
    {
        _baseComparer = baseComparer;
    }

    public int Compare(string x, string y)
    {
        // strings are completely same
        if (_baseComparer.Compare(x, y) == 0)
        {
            return 0;
        }

        // strings are completely same except last char
        if (_baseComparer.Compare(x.Substring(0, x.Length - 2), y.Substring(0, y.Length - 2)) == 0)
        {
            // if last char is E then win
            return x.Last() == 'E' ? -1 : 1;
        }
        // defaut compare everything else
        return _baseComparer.Compare(x, y);
    }
}

Then you are able doing this:

static void Main(string[] args)
    {
        List<string> list = new List<string>()
        {
            "DFG0001D",
            "AWK0007E",
            "ORK0127E",
            "AWK0007D",
            "DFG0001E",
            "ORK0127D"
        };
        list.Sort(new CustomStringComparer(StringComparer.CurrentCulture));
        foreach (var item in list)
        {
            Console.WriteLine(item);
        }
    }

And output is this:

AWK0007E
AWK0007D
DFG0001E
DFG0001D
ORK0127E
ORK0127D

Upvotes: 0

Risto M
Risto M

Reputation: 3009

Here is snippet how you can do this with Linq OrderBy and ThenByDescending operations:

string[] arr = { "DFG0001D", "AWK0007E", "ORK0127E", "AWK0007D", "DFG0001E", "ORK0127D" };

arr = arr
   .OrderBy(r => r.Substring(0, 7))
   .ThenByDescending(s => s.Substring(7, 1))
   .ToArray();

Upvotes: 5

coceban.vlad
coceban.vlad

Reputation: 509

    var list = new List<string>{
        "DFG0001D",
        "AWK0007E",
        "ORK0127E",
        "AWK0007D",
        "DFG0001E",
        "ORK0127D"
    };

    list.Sort((str1, str2) => {
        var eq = string.Compare(str1.Substring(0, str1.Length - 1), str2.Substring(0, str2.Length - 1));
        if (eq == 0)
            eq = string.Compare(str2[str2.Length - 1].ToString(), "E");
        return eq; 
    });
    foreach (var str in list)
        Console.WriteLine(str);

Output:

AWK0007E
AWK0007D
DFG0001E
DFG0001D
ORK0127E
ORK0127D

Upvotes: 0

Related Questions