C# 如何对列表进行排序<;字符串>;当数字是字符串的一部分时,按从低到高的数字排列?

C# 如何对列表进行排序<;字符串>;当数字是字符串的一部分时,按从低到高的数字排列?,c#,.net,winforms,C#,.net,Winforms,我有这样的分类方法: public List<string> SortList(List<string> thread) { thread = thread .OrderBy(str => { var match = Regex.Match(str, @"^([-+]?\d+)"); return match.Success ? int.Parse(match.Groups[1]

我有这样的分类方法:

public List<string> SortList(List<string> thread)
        {
            thread = thread
    .OrderBy(str =>
    {
        var match = Regex.Match(str, @"^([-+]?\d+)");
        return match.Success ? int.Parse(match.Groups[1].Value) : int.MaxValue;
    })
    .ToList();
            responsers.Add(new List<string>(thread));
            return thread;
        }

GetResponsers解析方法正在按我所希望的方式工作。问题是当数字是每个索引中字符串的一部分时,如何按数字对列表进行排序。

此代码在开始时适用于正的单个数字:

public List<string> SortList(List<string> thread)
{
    var first = thread.Take(1);
    var ordered = thread.Skip(1).OrderBy(s=>char.GetNumericValue(s[0]));
    thread = first.Concat(ordered).ToList();
    return thread;
}
公共列表排序列表(列表线程)
{
var first=thread.Take(1);
var ordered=thread.Skip(1).OrderBy(s=>char.GetNumericValue(s[0]);
thread=first.Concat(ordered.ToList();
返回线程;
}

你应该考虑让这个方法返回空洞BTW,所以这个< /P>

public void SortList(List<string> thread)
{
    var first = thread.Take(1);
    var ordered = thread.Skip(1).OrderBy(s=>char.GetNumericValue(s[0]));
    thread = first.Concat(ordered).ToList();
}
publicsvoidsortlist(列表线程)
{
var first=thread.Take(1);
var ordered=thread.Skip(1).OrderBy(s=>char.GetNumericValue(s[0]);
thread=first.Concat(ordered.ToList();
}
还是我最喜欢的

public List<string> SortList(List<string> thread)
{
    var first = thread.Take(1);
    var ordered = thread.Skip(1).OrderBy(s=>char.GetNumericValue(s[0]));
    return first.Concat(ordered).ToList();
}
公共列表排序列表(列表线程)
{
var first=thread.Take(1);
var ordered=thread.Skip(1).OrderBy(s=>char.GetNumericValue(s[0]);
return first.Concat(ordered.ToList();
}
尽管最后一个需要您将调用代码更改为
returnsortlist(threadList)

尝试以下操作:

List<string> list; // this is your input list

var firstRow = list.Take(1);
var orderedRows = list.Skip(1)
    .OrderBy(s=>Int32.Parse(s.Split(' ')[0].TrimEnd('.')));
var result = firstRow.Concat(orderedRows).ToList();
这样,您就不局限于以一位数字开头的字符串

所有输入字符串必须以数字开头,后跟点和空格,如下所示:

"1. "
输入示例:

"this is the title"
"6. this is after sorted"
"1. hello"
"2. hello world"
"82. fourth item digits are strings"
"3. this is third item"
"5. this is a test"
"731. this is the last item"
和输出:

"this is the title"
"1. hello"
"2. hello world"
"3. this is third item"
"5. this is a test"
"6. this is after sorted"
"82. fourth item digits are strings"
"731. this is the last item"

如果我是你,我会创建一个包含整数变量和字符串变量的类型,整数变量在开头存储数字。这样,您只需要按整数变量对列表进行排序。如果您不知道我的意思,我可以尝试为您创建一个示例。t_D是的,始终是一位数。t_D我在这部分中遇到错误:first.Concat(ordered)错误是:错误1成员“string.Concat(System.Collections.Generic.IEnumerable)”无法通过实例引用访问;使用类型名而不是headah sry来限定它,应该立即修复。不,它没有对它进行排序。我使用了您最喜欢的上一个示例,然后在GetResponsers方法中更改为:returnsortlist(threadList);但是,当我使用断点查看线程列表时,我在索引1“1.fdfgdfg”中看到,但在索引2中,我在索引3“15.fgfh”中看到“4.dfsdfd”,然后是“2.hhh”,然后是“3.ggg”,然后是“78.hello”,我希望您查看的是返回值,而不是threadList变量;)如果是的话,你有前导空格吗?我想你需要查看返回值:)谢谢。
"1. "
"this is the title"
"6. this is after sorted"
"1. hello"
"2. hello world"
"82. fourth item digits are strings"
"3. this is third item"
"5. this is a test"
"731. this is the last item"
"this is the title"
"1. hello"
"2. hello world"
"3. this is third item"
"5. this is a test"
"6. this is after sorted"
"82. fourth item digits are strings"
"731. this is the last item"