Select Same Element on Second List By Order of Second List

Viewed 161

I have a two list of character arrays:

char[] charA = new char[] { 'a', 'd', 'd', 'b', 'y', 'c', 'a' };
char[] charB = new char[] { 'a', 'b', 'c', 'd'};

Now I want to get all the same element and get the same arrangement like on second list (arrangement is a, b, c, d):

var final = charA.Where(x => charB.Any(y => y == x)).ToArray();

The result of this is:

'a', 'd', 'd', 'b', 'c', 'a'

But what I want is the arrangement on the second list using LINQ:

'a', 'a', 'b', 'c', 'd', 'd'
2 Answers
Related