List .Orderby 无法排序

.Net技术 码拜 8年前 (2016-03-05) 1106次浏览
            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 问一下不能排序是什么原因

解决方案

15

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

15

引用 1 楼 Forty2 的回复:

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

或tmpList.Sort((x,y)=>{ return x.MyID.CompareTo(y.MyID); });


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明List .Orderby 无法排序
喜欢 (0)
[1034331897@qq.com]
分享 (0)