public int Compare(Car x, Car y)
{
return x.Price -y.Price;
}
SortedList<String,Car> list=new SortedList<string,Car>();
list.Add(car1.CarBrand, car1);.....
list.Sort(new Comparison<Car>(Compare));
{
return x.Price -y.Price;
}
SortedList<String,Car> list=new SortedList<string,Car>();
list.Add(car1.CarBrand, car1);.....
list.Sort(new Comparison<Car>(Compare));
public int CompareTo(IComparable comp)
{
Car c= (Car)comp;
if (this.CarCost> c.CarCost)
return 1;
else if (this.CarCost== c.CarCost)
return 0;
else
return -1;
}
2楼的大哥虽然很谢谢你,但是没有用,你写的代码应该是在Car类里写的吧,但是用不了啊
这样试试:
SortedList<String,Car> list=new SortedList<string,Car>(new Sort());
public class Sort :IComparer
{ #region IComparer 成员 public int Compare(object x, object y)
{
Car carX = x as Car;
Car carY = y as Car;
return carX.CarCost - carY.CarCost;
} #endregion
}