How Sort A List By A Part Of That String Desc

前端 未结 6 1643
一整个雨季
一整个雨季 2021-01-23 00:09

i have a list like this :

List list_lines = new List();
list_lines.add(\"name1__pass1__com__14__55\");
list_lines.add(\"name2__pas         


        
6条回答
  •  野的像风
    2021-01-23 00:21

    You can take advantage of lambda expression in LINQ functions like OrderBy

    string[] Separator = new string[] { "__" };
    var sortedList = list_lines
        .OrderBy(s => int.Parse(s.Split(Separator, StringSplitOptions.None)[4]))
        .ToList();
    

    As an unrelated side note, please use correct C# naming conventions so your code is easier to read and is unified with existing C# code-base. E.g. not beginning local variable names with capital letter (Separator -> separator) and using lower camel case (Pascal case) in case it contains more words (list_lines -> listLines)

提交回复
热议问题