2015-11-13 114 views
-2

我有一個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

如果這是關於正確命令基於一系列表單字段的驗證輸出,我有下沉的感覺,你會走錯方向。 – flq

+0

我不認爲你想要/需要這個正則表達式 - 我可能會使用[字符串]列表,然後以編程方式重新排序。另外,作爲一個附註(我沒有投票給你) - 我的名字包含4個字母,這會讓我感到沮喪。 – Nefariis

回答

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(); 

您可能需要修改該代碼,如果線路不包含您所設定的關鍵字...

BTW:你的問題是不清晰的像

Wrong Email or Fax number 
的情況下,