有没有办法按字符串数组匹配的单词数对字符串列表进行排序?
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);
}
Run Code Online (Sandbox Code Playgroud)
有没有办法生成输出而不使用另一个list
或for
循环来排序
one two three little pony
one two little pony
one little pony
little pony
Run Code Online (Sandbox Code Playgroud)
使用Count
而不是Any
:
x = x.OrderByDescending(u => targets.Count(u.Contains)).ToList();
Run Code Online (Sandbox Code Playgroud)