I have a list of strings that I want to sort by another list of strings and if there are items in the list that is not in the comparer list I want them as they are at the end of the list. How can I do this?
So if I have this list: List<string> {"A","T","G","F","V","E","W","Q" }
and I have a comparer: List<string> {"T","F","V","A" }
I want the result to be: List<string> {"T","F","V","A","G","E","W","Q" }
Thanks!
It looks like you need to use Intersect
and Except
like:
List<string> originalList = new List<string> {"A", "T", "G", "F", "V", "E", "W", "Q"};
List<string> compareList = new List<string> {"T", "F", "V", "A"};
var intersectedItems = compareList.Intersect(originalList);
var notIntersectedItems = originalList.Except(compareList);
var resultList = intersectedItems.Concat(notIntersectedItems).ToList();
You will get:
resultList = T,F,V,A,G,E,W,Q
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With