Is possible to order on Linq according to a specific order?
something like
List<bbHeader> bb = new List<bbHeader>();
bb.OrderBy(x => x.Country.CompareTo(new string[]{"AR","CL","PY","UY","AUP"}));
The idea is that the Country field is ordered according to the specific order of the string
There’s a very direct way in your example:
In this way, you are ordering by the index Country is found in the sequence string. Just keep in mind that not-found items will be -1, which you can correct for also if you like.
If you want to do anything more complex, you can to create your own custom
IComparerclass implementation to compare the items using your custom order. This can then be passed into OrderBy.Such an IComparer would look like:
And can be called like:
Either way works well, the latter is nice and reusable, but the former (using IndexOf directly) is still very concise as well. Your choice.