2016-06-09 23 views
-1

我有一個列表字符串: [「a1」,「b0」,「c0」,「a2」,「c1」,「d3」,「a3 「。 我想根據它們的後綴得到一個列表[「a3」,「d3」,「c1」,「b0」]。 例如:「a1」,「a2」,「a3」。結果是「a3」。 這個問題可能很簡單,但我解決不了。 感謝您的幫助!C#查找列表字符串元素,它們的後綴大於其他

+0

因爲我沒有正確的方法來解決這個問題,所以沒有代碼提供。我的問題是明確的嗎?你能告訴我你的方式嗎? – binh

+0

你的問題不清楚,不。爲什麼a3,d3,c1和b0特別?你沒有給我們製作輸出的規則。 – Rob

+0

對不起,我的錯誤,[「a3」,「d3」,「c1」,「b0」] – binh

回答

2

正在關注Linq聲明可以滿足您的需求。

var result= input.Select(x=> new {letter = x[0], number = x[1], item=x}) // Separate letter & number. 
     .GroupBy(x=>x.letter)           // Group on letter and take first element (of max number) 
     .Select(x=> x.OrderByDescending(o=>o.number).First())   
     .OrderByDescending(x=>x.number)         // Order on number. 
     .Select(x=>x.item)            // get the item. 
     .ToArray(); 

輸出

[ 
    a3 
    , 
    d3 
    , 
    c1 
    , 
    b0 
] 

入住這Example

+0

它分配較短的解決方案,然後我將發佈。鑑於問題的簡單性,我只是想使用嵌套for循環,因爲我懷疑他/她能理解LINQ –

+0

這是一個好主意,它對初學者很有幫助。 –

+0

也@binh你應該知道當你有多個數字時,代碼只會識別第一個數字 –

0

下面是一個另類,它很長,主要是因爲我試圖解釋每一行

// create list based on your original text 
var list = new List<string> { "a1", "b0", "c0", "a2", "c1", "d3", "a3" }; 

// use a dictionary to hold the prefix and max suffixes 
var suffixMaxDictionary = new Dictionary<string, int>(); 

// loop through the list 
for (int i = 0; i < list.Count; i++) 
{ 
    // get the prefix using Substring() 
    var prefix = list[i].Substring(0, 1); 

    // if the prefix already exist in the dictionary then skip it, it's already been processed 
    if (suffixMaxDictionary.ContainsKey(prefix)) 
     continue; // continue to the next item 

    // set the max suffix to 0, so it can be checked against 
    var suffixMax = 0; 

    // loop through the whole list again to get the suffixes 
    for (int j = 0; j < list.Count; j++) 
    { 
     // get the current prefix in the second loop of the list 
     var thisprefix = list[j].Substring(0, 1); 

     // if the prefixes don't match, then skip it 
     // e.g. prefix = "a" and thisprefix = "b", then skip it 
     if (prefix != thisprefix) 
      continue; 

     // get the suffix 
     // warning though, it assumes 2 things: 
     // 1. that the second character is a number 
     // 2. there will only ever be numbers 0-9 as the second character 
     var thisSuffix = Convert.ToInt32(list[j].Substring(1, 1)); 

     // check the current suffix number (thisSuffix) compared the suffixMax value 
     if (thisSuffix > suffixMax) 
     { 
      // if thisSuffix > suffixMax, set suffixMax to thisSuffix 
      // and it will now become the new max value 
      suffixMax = thisSuffix; 
     } 
    } 
    // add the prefix and the max suffix to the dictionary 
    suffixMaxDictionary.Add(prefix, suffixMax); 
} 
// print the values to the console 
Console.WriteLine("original: \t" + string.Join(",", list)); 
Console.WriteLine("result: \t" + string.Join(",", suffixMaxDictionary)); 
Console.ReadLine(); 

參見https://dotnetfiddle.net/BmvFEp ,th anks @Hari普拉薩德,我不知道有用於.NET

+0

謝謝你的回答。 我通過你的例子學到了很多東西。 – binh

0

這將使你在問題中描述的最大的「後綴」的第一個實例小提琴:

string[] test = { "a3", "d3", "c1", "b0" }; 
string testResult = test.FirstOrDefault(s => s.Last<char>() == s.Max(t => s.Last<char>())); 

在這種情況下,結果是「a3」

相關問題