ProductCollection products = this.pagedDataSource.DataSource as ProductCollection;
switch (this.sortBy)
{
case "Cheapest":
products.Sort(delegate (Product p1, Product p2) {
return p1.OurPrice.CompareTo(p2.OurPrice);
});
return;
case "Expensive":
products.Sort(delegate (Product p1, Product p2) {
return p2.OurPrice.CompareTo(p1.OurPrice);
});
return;
case "TitleAsc":
products.Sort(delegate (Product p1, Product p2) {
return p1.Name.CompareTo(p2.Name);
});
return;
case "TitleDesc":
products.Sort(delegate (Product p1, Product p2) {
return p2.Name.CompareTo(p1.Name);
});
return;
case "Newest":
products.Sort(delegate (Product p1, Product p2) {
return p2.CreatedOn.CompareTo(p1.CreatedOn);
});
return;
case "Oldest":
products.Sort(delegate (Product p1, Product p2) {
return p1.CreatedOn.CompareTo(p2.CreatedOn);
});
return;
}
}
这是dashcommerce中的代码。我不明白那个return返回的都是个整数。要这些个case是怎样实现的排序?
对于products.Sort(delegate (Product p1, Product p2) {
return p1.CreatedOn.CompareTo(p2.CreatedOn);
});
你就要查看一下Sort的声明了,它接收的是一个方法(委托或Lambda等)return的是委托中的返回。你就查看一下Sort的声明与使用吧
products.Sort(
QuickSort
用的是委托,通过比较2个对象,进行排序