最佳答案
您可以实现IComparable接口,如sugested here:
public class Temperature : IComparable
{
// The temperature value
protected double temperatureF;
public int CompareTo(object obj) {
if (obj == null) return 1;
Temperature otherTemperature = obj as Temperature;
if (otherTemperature != null)
return this.temperatureF.CompareTo(otherTemperature.temperatureF);
else
throw new ArgumentException("Object is not a Temperature");
}
...
您将使用CompareTo方法来比较您班级的项目.更多关于IComparable的信息可以在SO找到.有了比较你可以根据比较功能对你的对象列表进行排序,如上所述here
相关文章
转载注明原文:c# – 如何比较同一类的两个对象? - 代码日志