我在C#中有一个字符串列表。它在列表中的字符串中间的某处包含以下子字符串。该清单未订购。我想根据其中的以下条目订购此列表:
Email FirstName LastName Country Province Phone Fax
按此顺序。
例如,如果原始列表包含以下条目:
The field Province required 5 letters Wrong Email Phone is required FirstName should contain min of 5 letters The field Fax requires 10 digits Enter a valid LastName Fax is required The field Phone requires 10 digits
需要按如下方式排序:
Wrong Email FirstName should contain min of 5 letters Enter a valid LastName The field Province required 5 letters Phone is required The field Phone requires 10 digits Fax is required The field Fax requires 10 digits
由于
答案 0 :(得分:1)
可能是这样的。
var order = new List<string>()
{
"Email",
"FirstName",
"LastName",
"Country",
"Province",
"Phone",
"Fax",
};
var lines = new List<string>()
{
"The field Province required 5 letters",
"Wrong Email",
"Phone is required",
"FirstName should contain min of 5 letters",
"The field Fax requires 10 digits",
"Enter a valid LastName",
"Fax is required",
"The field Phone requires 10 digits",
};
Func<string, int> calc = s => Regex.Matches(s, @"\w+").Cast<Match>()
.Select(m => m.Value)
.Max(x => order.IndexOf(x));
var result = lines.OrderBy(l => calc(l)).ToList();
如果某行不包含您设置中的任何关键字,则可能需要修复此代码...
顺便说一句:对于像
这样的案例,您的问题并不明确Wrong Email or Fax number