【问题标题】:<List> Sort help C# (unity)<List> 排序帮助C#(统一)
【发布时间】:2013-10-16 20:39:37
【问题描述】:

我需要帮助对我拥有的列表进行排序....

我有这份清单

List<List<string>> PlayerList= new List<List<string>>();

填充后我会有类似的东西

[123112412,John,225,123.5522,Dalas]
[123312,Fred,12,43.5522,Chicago]

然后我可以访问例如:

PlayerList[0][1] = John
PlayerList[1][1] = Fred

现在的问题...我需要按第 4 个字段对这个“PlayerList”进行排序...我该怎么做...我真的迷失了 C#。

我需要输出类似于:

[123312,Fred,12,43.5522,Chicago]
[123112412,John,225,123.5522,Dalas]

提前致谢。

【问题讨论】:

  • 我认为排序后的列表将是sorted = PlayerList.OrderBy(list=&gt;list[4]).ToList();
  • 无需在标题中包含 C# / Unity,因为您将它们作为标签
  • 对不起,第一个问题...
  • 不要忘记您可以定义自己的数据结构。 List&lt;List&lt;string&gt;&gt; 可以很容易地成为 List&lt;MyCustomClass&gt;。哪个更清楚:PlayerList[0][1]PlayerList[0].name
  • 很好......我也试试...... tks

标签: c# list sorting unity3d sql-order-by


【解决方案1】:
var newList = PlayerList.OrderBy(x => x[4]).ToList();


foreach (var line in newList)
{
    Console.WriteLine(string.Join(",", line));
}

【讨论】:

  • 我该如何投射,或者说它是这个字段的数字?因为它作为字符串排序......
  • @FlavioEngel 你的第四个字段是 string (Chicago/Dalas)
  • @FlavioEngel var newList = PlayerList.OrderBy(x => double.Parse(x[3],CultureInfo.InvariantCulture)).ToList();
  • 非常感谢 L.B.,C# 对我来说还是很新...
猜你喜欢
  • 2011-05-10
  • 2011-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-22
相关资源
最近更新 更多