user626528 - 7 months ago 47

C# Question

I have an array of unique values specified in some arbitrary order; I have another array of (nearly) the same values as the first array, but some values may be missing, and they are ordered in a completely different way.

How do I order values in the second array in the same order as the first one?

UPD for example:

array 1: 4, 8, 2, 5, 9, 3

array 2: 5, 8, 4, 2

required result: 4, 8, 2, 5

Answer

You could check that each element in array 1 exists in array 2 and take them in that order:

```
var result = array1.Where(a => array2.Contains(a));
```

Source (Stackoverflow)