Well, I have a class Human that implement IComparable<Human>
Then I have two more classes that inherit from Human Child:Human and Cousin:Human
The Parent class has a property AGE that has in the getter a call to a function getAge() which is abstract.
I have a List of Humans and when I display them in a datagrid every age is calculated properly.
I want to sort the list using age as attribute so I make the Human abstract class to implement Icomparable and then the method like this.
public int CompareTo(Human other)
{
return this.age.CompareTo(other.age);
}
I invoke the list.sort()
method in the ASP like this
List<Human> hlist = instance.humanlist;
hlist.Sort();
tblHumans.DataSource = hlist;
tblHumans.DataBind();
The page loads with all the data but the items are not ordered by age, it seems ordered by position in the list.
My tblHumans is
<asp:GridView ID="tblHumans" runat="server">
</asp:GridView>
In the Parent class the attribute AGE is like this
public int Age
{
get
{
return getAge();
}
set
{
age = getAge();
}
}
getAge() is an abstract method that my child classes overrides
The calculation is returns values correctly, when the table is rendered every single value is there with the right results.
What I am doing wrong?
Ok, I fix it
thank you all for the time you took reading and answering this question.
At last I made this:
1) set again the age property and field with normal getter and setter 2) in the child class when I override the getAge method I force set the age too, so, when the compareTo is called in the sort method of the list the age property is populated so it is shown in the table.
I know it is not 'the best practic' but It has to be made this way because in this program I cannot use 'complex' data models (not even a database, but dont worry is not for a production system)
Thanks all again