IComparer中的问题< T> [英] Problem in IComparer<T>
本文介绍了IComparer中的问题< T>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,
我无法在Sort方法中将Person类中的对象放入。
使用System ;
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;
名称空间P960909_P1
{
类人物:IComparer< Person>
{
public string Name {get;组; }
public string Family {get;组; }
public int Age {get;组; }
public int Compare(Person p1,Person p2)
{
return p1.Age.CompareTo(p2.Age);
}
}
class FirstName:IComparer< Person>
{
public int Compare(Person p1,Person p2)
{
return p1.Name.CompareTo(p2.Name);
}
}
class LastName:IComparer< Person>
{
public int Compare(Person p1,Person p2)
{
return p1.Family.CompareTo(p2.Family);
}
}
类年龄:IComparer< Person>
{
public int Compare(Person p1,Person p2)
{
return p1.Age.CompareTo(p2.Age);
}
}
class程序
{
static void Main(string [] args)
{
List< Person> ; p =新列表< Person>
{
new Person {Name =" Alex" ,Family ="T" ,年龄= 27},
新人{Name =" Hadi" ,Family =" eRam" ,年龄= 20},
新人{姓名=" A" ,Family =" B" ,年龄= 50},
};
Console.WriteLine(" Before Sorting ....");
foreach(p中的人物项目)
{
Console.Write(" Name:{0} \tFamily:{1} \tAge:{2} \ n", item.Name,item.Family,item.Age);
}
IComparer< Person> compareAge = new Age();
//p.Sort(p);有错误
p.Sort(compareAge);
Console.WriteLine(" After ----------");
foreach(p中的var项目)
{
Console.Write(" Name:{0} Family:{1}年龄:{2} \ n", item.Name,item.Family,item.Age);
}
Console.ReadKey();
}
}
}
解决方案
推荐继承自
Comparer< T>并覆盖Compare()而不是实现IComparer< T>直接点。
Hello,
I can't put object from Person class in Sort method.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace P960909_P1 { class Person : IComparer<Person> { public string Name { get; set; } public string Family { get; set; } public int Age { get; set; } public int Compare(Person p1, Person p2) { return p1.Age.CompareTo(p2.Age); } } class FirstName : IComparer<Person> { public int Compare(Person p1 , Person p2) { return p1.Name.CompareTo(p2.Name); } } class LastName : IComparer<Person> { public int Compare(Person p1 , Person p2) { return p1.Family.CompareTo(p2.Family); } } class Age : IComparer<Person> { public int Compare(Person p1 , Person p2) { return p1.Age.CompareTo(p2.Age); } } class Program { static void Main(string[] args) { List<Person> p = new List<Person> { new Person { Name = "Alex" , Family = "T" , Age = 27}, new Person { Name = "Hadi" , Family = "eRam" , Age = 20 }, new Person { Name = "A" , Family = "B" , Age = 50 }, }; Console.WriteLine("Before Sorting ...."); foreach (Person item in p) { Console.Write("Name:{0}\tFamily:{1}\tAge:{2}\n",item.Name,item.Family,item.Age); } IComparer<Person> compareAge = new Age(); //p.Sort(p); Have an error p.Sort(compareAge); Console.WriteLine("After ----------"); foreach (var item in p) { Console.Write("Name:{0} Family:{1} Age:{2}\n", item.Name, item.Family, item.Age); } Console.ReadKey(); } } }
解决方案
It is recommended to inherit from Comparer<T> and override Compare() instead of implementing IComparer<T> directly.
这篇关于IComparer中的问题< T>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文