Q:

Linq OrderBy custom order

Say I have the following char 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

A:

May be 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); 
}

A:

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

A:

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

