How Sort A List By A Part Of That String Desc

前端 未结 6 1646
一整个雨季
一整个雨季 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:34

    The other answers create a new list which is sorted the way you want. If instead you want the same list to be sorted, maybe try something like this:

    Func getNum = str => int.Parse(str.Split(Separator, StringSplitOptions.None)[4]);
    list_lines.Sort((x, y) => getNum(x).CompareTo(getNum(y)));
    

    This uses an overload of List<>.Sort. If you want descending order, swap x and y in the Comparison<> lambda body.

    If your list is very long, this is faster (uses Quick Sort) and doesn't require the memory of a new copy of the list.

提交回复
热议问题