How do you sort a String List by the number of words matched with an Array in Linq

c#, linq

Solution

Use `Count` instead of `Any`:

x = x.OrderByDescending(u => targets.Count(u.Contains)).ToList();

Problem

Is there a way to sort a String List by the number of words matched from a string array? ``` var targets = new string[] { "one", "two", "three" }; var list = new List<string>(); list.Add("one little pony"); list.Add("one two little pony"); list.Add("one two three little pony"); list.Add("little pony"); x = x.OrderByDescending(u => targets.Any(u.Contains)).ToList(); foreach(var item in list) { Debug.Writeline(item); } ``` Is there a way to generate an output without using another `list` or `for` loop to sort ``` one two three little pony one two little pony one little pony little pony ```

Original source