I'm try to make a generic sort function to sort my class- "GiaoTrinh" by attribute price_GT use delegate. In class GiaoTrinh i created a function that called is CheckPrice to compare price between to object.In class ListGiaoTrinh i created a delegate and function GenericSort with parameters is List<object>
.When i call GenericSort
function the error is :
"cannot convert from
Generic.List<GiaoTrinh>
toGeneric.List<object>
.
How can i resolve it
class GiaoTrinh:IGiaoTrinh
{
private int id_GT;
private string name_GT;
private string tacGia;
private int namXuatBan;
private double price_GT;
public static bool CheckPrice(object a,object b)
{
if (((GiaoTrinh)a).Price_GT < ((GiaoTrinh)b).Price_GT)
return true;
else
return false;
}
}
Code in class ListGiaoTrinh:
class ListGiaoTrinh:IListGiaoTrinh
{
public delegate bool compare(object o1, object o2);
public static void GenericSort(List<object>obj, compare cmp)
{
for (int i = 0; i < obj.Count - 1; i++)
for (int j = i + 1; j < obj.Count; j++)
{
if (cmp(obj[i], obj[j]))
{
object temp;
temp = obj[i];
obj[i] = obj[j];
obj[j] = temp;
}
}
}
private List<GiaoTrinh> listGiaoTrinh;
public void Sort()
{
GenericSort(this.listGiaoTrinh, GiaoTrinh.CheckPrice);
Show();
}
}