List .Orderby 无法排序???

baidu_26716013 2015-10-27 10:01:06
MyClass tmpClass = new MyClass();
List<MyClass> tmpList = new List<MyClass>();
tmpClass.MyID = "5";
tmpClass.RandomCode = "555";
tmpList.Add(tmpClass);

tmpClass = new MyClass();
tmpClass.MyID = "3";
tmpClass.RandomCode = "333";
tmpList.Add(tmpClass);

tmpClass = new MyClass();
tmpClass.MyID = "1";
tmpClass.RandomCode = "111";
tmpList.Add(tmpClass);

tmpClass = new MyClass();
tmpClass.MyID = "4";
tmpClass.RandomCode = "444";
tmpList.Add(tmpClass);

tmpClass = new MyClass();
tmpClass.MyID = "2";
tmpClass.RandomCode = "222";
tmpList.Add(tmpClass);

tmpList.OrderBy(p => p.MyID).ToList();

查看结果,排序还是 5 3 1 4 2 ?

我想把里面的 排序成 12345 请问不能排序是什么原因
...全文
296 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Ny-6000 2016-06-02
  • 打赏
  • 举报
回复
baidu_26716013 2015-10-27
  • 打赏
  • 举报
回复
引用 3 楼 angel6709 的回复:
[quote=引用 1 楼 Forty2 的回复:] tmpList = tmpList.OrderBy(p => p.MyID).ToList();
或tmpList.Sort((x,y)=>{ return x.MyID.CompareTo(y.MyID); });[/quote] 谢谢你的教学 ^^
angel6709 2015-10-27
  • 打赏
  • 举报
回复
引用 1 楼 Forty2 的回复:
tmpList = tmpList.OrderBy(p => p.MyID).ToList();
或tmpList.Sort((x,y)=>{ return x.MyID.CompareTo(y.MyID); });
baidu_26716013 2015-10-27
  • 打赏
  • 举报
回复
引用 1 楼 Forty2 的回复:
tmpList = tmpList.OrderBy(p => p.MyID).ToList();
原来.....我以为跟 .add 一样直接执行了 谢谢你
Forty2 2015-10-27
  • 打赏
  • 举报
回复
tmpList = tmpList.OrderBy(p => p.MyID).ToList();

110,561

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧