9

Say I have the following character array:

char[] c = new char[] { 'G', 'R', 'D', 'D', 'G', 'R', 'R', 'C', 'D', 'G', 'R', 'R', 'C', 'G', 'R', 'D', 'D', 'G', 'R', 'R', 'C', 'D', 'G', 'R', 'R', 'C' };


c.OrderBy(y => y).ForEach(x => Console.WriteLine(x));
//CCCCDDDDDDGGGGGGRRRRRRRRRR

How do I use LINQ to produce the following order?

//DDDDDDGGGGGGRRRRRRRRRRCCCC
4

6 Answers 6

38

Maybe you want to do something like this:

char [] customOrder = { 'D', 'G', 'R', 'C'};
char [] c = new char[] { 'G', 'R', 'D', 'D', 'G', 'R',
                         'R', 'C', 'D', 'G', 'R', 'R',
                         'C', 'G', 'R', 'D', 'D', 'G',
                         'R', 'R', 'C', 'D', 'G', 'R', 'R', 'C' };

foreach (char item in c.OrderBy(ch => Array.IndexOf(customOrder, ch))) {
    Console.Write(item);
}
7
  • @TimSchmelter he is using ForEach LINQ extension method.
    – Euphoric
    Feb 26, 2015 at 12:45
  • 1
    @Euphoric: there is no ForEach extension method apart from one that he might have written from the scratch. stackoverflow.com/a/200584/284240 Feb 26, 2015 at 12:47
  • @TimSchmelter msdn.microsoft.com/query/… Feb 26, 2015 at 12:48
  • @KamelBRAHIM: that is not an extension method but an instance method of List<T>. Feb 26, 2015 at 12:49
  • 1
    @KamelBRAHIM: no, but you have said that it's an extension method and it is not. It was there since .NET 2(so even before LINQ). It's important because OP uses OrderBy(...).ForEach and this answer used it also. Using ToList().ForEach is a bad habit because you are looping all items in the sequence to create a new collection from the scratch, just to enable to loop it again in ForEach. Use foreach if you want to loop(consume) a query. Feb 26, 2015 at 12:55
7

You could use another collection which defines the order:

char[] order = {'D','G','R','C'};
var customOrder = c.OrderBy(chr =>
{
    int index = Array.IndexOf(order, chr);
    if (index == -1) return int.MaxValue;
    return index;
});
5

If you want to define the order as relation between the items, then you have to use IComparer with the other OrderBy method.

public class Comparer : IComparer<char>
{
    public int Compare(Char a, Char b)
    {
       //return positive if a should be higher, return negative if b should be higher
    }
}

c.OrderBy(c => c, new Comparer()).ForEach(x => Console.WriteLine(x));
4

My own solution (thank you to those who led me in the right direction)

char[] c = new char[] { 'G', 'R', 'D', 'D', 'G', 'R', 'R', 'C', 'D', 'G', 'R', 'R', 'C', 'G', 'R', 'D', 'D', 'G', 'R', 'R', 'C', 'D', 'G', 'R', 'R', 'C' };
c.OrderBy(x => "CDRG".IndexOf(x)).ForEach(Console.Write);

Produces:

CCCCDDDDDDRRRRRRRRRRGGGGGG

2

You can use a dictionary with the order of the elements:

Dictionary<char, int> order = new Dictionary<char,int> {
    { 'D', 0 },
    { 'G', 1 },
    { 'R', 2 },
    { 'C', 3 },
};

char[] c = new char[] { 'G', 'R', 'D', 'D', 'G', 'R', 'R', 'C', 'D', 'G', 'R', 'R', 'C', 'G', 'R', 'D', 'D', 'G', 'R', 'R', 'C', 'D', 'G', 'R', 'R', 'C' };

// Here we search the dictionary for the "order" to be used
// and we compare the value with the value of the other 
// compared item
Array.Sort(c, (p, q) => order[p].CompareTo(order[q]));

var str = new string(c);
Console.WriteLine(str);
2

I'd approach this with a join ensuring that the ordering array is on the left side of the join.

var ordering = "CDGR".ToCharArray();

var orderedOutput = ordering.Join(c, a => a, b => b, (a, b) => b);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.