我有一个List<string>看起来像这样的对象:
option 1
line a
line b
line c
option 3
line x
line z
line y
option 2
a1
a4
a2
a3
Run Code Online (Sandbox Code Playgroud)
所有辅助细节都缩进了一个空格.是否可以使用LINQ对此列表进行排序?我只知道非常基本的排序,并且在尝试对第一列中包含空格的字符串的辅助行进行排序时,并不知道从哪里开始.
以下是我希望List在排序后的样子
option 1
line a
line b
line c
option 2
a1
a2
a3
a4
option 3
line x
line y
line z
Run Code Online (Sandbox Code Playgroud)
我刚刚更新了这个问题.我有,List<string>但发表后它被砍掉了.
使用:
yourEnumerable.OrderBy(y => y.Key).ThenBy(y => y.Prop2)
Run Code Online (Sandbox Code Playgroud)
请参阅MSDN上的文档:http://msdn.microsoft.com/en-us/library/bb534966.aspx和http://msdn.microsoft.com/en-us/library/bb534743.aspx
更新: 尝试下面的代码来解析和排序,它有点伪:
string lineRead;
string current;
var dictionary = new Dictionary<string, List<string>>);
while((lineRead = reader.ReadLine())
{
if(!lineRead.StartsWith(" "))
{
dictionary.Add(lineRead, new List<string>());
current = lineRead;
}
else
{
dictionary[current].Add(lineRead);
}
}
dictionary.Keys.ToList().ForEach(y => dictionary[y] = dictionary[y].OrderBy(x => x).ToList());
dictionary.OrderBy(y => y.Key);
Run Code Online (Sandbox Code Playgroud)