排序有两个标准,串升,INT上升 [英] Sort with two criteria, string ascending, int ascending
问题描述
我怎么能在两个不同的标准进行排序
例如,我有Person对象,如:
人
具有属性姓
(串),姓
和等级
(INT)
示例数据,像这样:
泽维尔·史密斯1
亚历山大·史密斯2
亚历山大·史密斯1
霍克2
应该排序按字母顺序名字,然后在等级,如结果:
亚历山大·史密斯1
亚历山大·史密斯2
霍克2
泽维尔·史密斯1
到目前为止,我曾尝试以下,但它不能正常工作:
peopleList
是列表<人>
peopleList.Sort(新比较和LT;人>((X,Y)=> x.Rank.CompareTo(y.Rank)));
peopleList.Sort(新比较和LT;人>((X,Y)=>的String.Compare(x.Name,y.Name)));
感谢
修改: ,以避免更改我的代码太多了,我真的想保持列表中,如果我更改上面的线为:
peopleList.OrderBy(人=> person.FirstName).ThenBy(人=> person.Rank).ToList();
将给予相同的列表只是排序正确,是否正确?
< DIV CLASS =h2_lin>解决方案
LINQ方法
使用LINQ可以使用的OrderBy 并的 ThenBy :
VAR的结果= peopleList.OrderBy(p => p.FirstName).ThenBy (p值=> p.Rank);
这将返回一个的IEnumerable< T>
。如果你真的需要一个列表< T>
添加 .ToList()
末
如果你想使用排序
方法,那么你就需要编写一个自定义比较。
编辑:使用了ToList()
返回一个新的列表。如果要排序现有的名单,那么你应该使用哪一个不返回一个列表,而是操作的当前列表中的排序
方法(它是一个无效
法)。
排序/的Comparer方法
使用: list.Sort(新PersonComparer());
下面是比较器的代码。它是从适应MSDN例如的,所以我建议你阅读他们用评论来了解为什么它是。这种方式构造
公共类PersonComparer:的IComparer<&人GT;
{
公众诠释比较(人X,人Y)
{
如果(X == NULL)
{
如果(Y == NULL )
{
返回0;
}
,否则
{
返回-1;
}
}
,否则
{
如果(Y == NULL)
{
返回1;
}
,否则
{
INT RETVAL = x.FirstName.CompareTo(y.FirstName);
如果(RETVAL!= 0)
{
返回RETVAL;
}
,否则
{
返回x.Rank.CompareTo(y.Rank);
}
}
}
}
}
How can I perform a sort on two different criteria?
For example, I have person objects like:
Person
with properties FirstName
(string), LastName
, and Rank
(int).
Example data like so:
Xavier Smith 1
Alexander Smith 2
Alexander Smith 1
Bob Hawke 2
It should sort on FirstName alphabetically, then on rank, e.g. resulting:
Alexander Smith 1
Alexander Smith 2
Bob Hawke 2
Xavier Smith 1
So far, I have tried the following, but it isn't working properly:
peopleList
is 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)));
Thanks
edit: to avoid changing my code too much, I really want to keep list, if I change the above lines to:
peopleList.OrderBy(person => person.FirstName).ThenBy(person => person.Rank).ToList();
Would give the exact same list just sorted properly, correct?
LINQ Approach
With LINQ you can use OrderBy and ThenBy:
var result = peopleList.OrderBy(p => p.FirstName).ThenBy(p => p.Rank);
This will return an IEnumerable<T>
. If you really need a List<T>
add a .ToList()
at the end.
If you want to use the Sort
method then you'll need to write a custom comparer.
EDIT: using ToList()
returns a new list. If you want to sort your existing list then you should use the Sort
method which does not return a list but rather operates on the current list (it is a void
method).
Sort / Comparer Approach
Use: list.Sort(new PersonComparer());
Here's the comparer code. It was adapted from the MSDN example so I recommend reading the comments they used to understand why it is structured this way.
public class PersonComparer : IComparer<Person>
{
public int Compare(Person x, Person y)
{
if (x == null)
{
if (y == null)
{
return 0;
}
else
{
return -1;
}
}
else
{
if (y == null)
{
return 1;
}
else
{
int retval = x.FirstName.CompareTo(y.FirstName);
if (retval != 0)
{
return retval;
}
else
{
return x.Rank.CompareTo(y.Rank);
}
}
}
}
}
这篇关于排序有两个标准,串升,INT上升的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!