如何对两个不同的标准执行排序?按照两个标准排序,字符串升序,int升序
例如,我有人对象像:
Person
具有属性FirstName
(字符串),LastName
,和Rank
(INT)。
实施例的数据如下所示:
Xavier Smith 1
Alexander Smith 2
Alexander Smith 1
Bob Hawke 2
应该排序姓按字母顺序,然后在等级,例如由此而来:
Alexander Smith 1
Alexander Smith 2
Bob Hawke 2
Xavier Smith 1
到目前为止,我曾尝试以下,但它不能正常工作:
peopleList
是List<Person>
peopleList.Sort(new Comparison<Person>((x,y) => x.Rank.CompareTo(y.Rank)));
peopleList.Sort(new Comparison<Person>((x, y) => string.Compare(x.Name, y.Name)));
感谢
编辑:到避免更改我的代码太多,我真的想保留列表,如果我将上面的行更改为:
peopleList.OrderBy(person => person.FirstName).ThenBy(person => person.Rank).ToList();
会给出刚刚排序完全相同的列表,正确吗?
我真的必须阅读更多关于LINQ,它似乎让我的生活变得如此简单! – baron 2010-03-04 03:23:27
@baron查看我的编辑,我在最近的编辑中对您的问题进行了评论。 – 2010-03-04 03:25:11
@baron我添加了一个可以和'Sort()'一起使用的比较器。 – 2010-03-04 03:36:59