【发布时间】:2020-06-02 07:58:17
【问题描述】:
我有一个看起来像这样的字符串列表(完全未排序):
[Title b] \t [text] \t complete
[Title a] \t [text] \t incomplete
[Title a] \t [text] \t complete
[Title c] \t [text] \t complete
...
它应该被排序为如下所示:
[Title a] \t [text] \t complete
[Title a] \t [text] \t incomplete
[Title b] \t [text] \t complete
[Title c] \t [text] \t complete
...
或者这个(排序方式可以选择)
[Title a] \t [text] \t complete
[Title j] \t [text] \t complete
[Title s] \t [text] \t complete
...
[Title b] \t [text] \t incomplete
...
我对整个排序的方法有点肮脏,一点也不圆滑,而且只解决了第二种排序方式。
List<string> sortedTps = new List<string>();
List<string> completeTps = new List<string>();
List<string> incompleteTps = new List<string>();
List<string> errorTps = new List<string>();
foreach (string tp in tpByProduct)
{
if (tp.Contains("\t complete"))
{
completeTps.Add(tp);
}
else if (tp.Contains("\t incomplete"))
{
incompleteTps.Add(tp);
}
}
completeTps.Sort();
incompleteTps.Sort();
foreach(string tp in completeTps)
{
sortedTps.Add(tp);
}
foreach(string tp in incompleteTps)
{
sortedTps.Add(tp);
}
希望你们能告诉我如何更好更短地做到这一点^^
【问题讨论】:
-
您可以为此编写自己的比较器。能分享一下具体的排序规则吗?
errorTps是什么?
标签: c# list if-statement foreach