Hi,
I have a bit of an odd problem. I have a grid that i must load some data to and sort it by date. I implemented CompareTo but it doesn't seem to work at all.
The objects in the grid seem to be put there at random. I tried to implement compareto in 3 ways. None of them worked. I do the sorting before i bind to the grid.
The code for the compareto method is this:
- if (this.LastStatusDate.Year > to.LastStatusDate.Year) return 1;
- else return 0;
- if ((this.LastStatusDate.Year == to.LastStatusDate.Year) &&
- (this.LastStatusDate.Month > to.LastStatusDate.Month)) return 1;
- else return 0;
- if ((this.LastStatusDate.Year == to.LastStatusDate.Year) &&
- (this.LastStatusDate.Month == to.LastStatusDate.Month) &&
- (this.LastStatusDate.Day > to.LastStatusDate.Day)) return 1;
- else return 0;
- if ((this.LastStatusDate.Year == to.LastStatusDate.Year) &&
- (this.LastStatusDate.Month == to.LastStatusDate.Month) &&
- (this.LastStatusDate.Day == to.LastStatusDate.Day) &&
- (this.LastStatusDate.Hour > to.LastStatusDate.Hour)) return 1;
- else return 0;
- }
The other 2 versions i tried were:
public int CompareTo(Issue to) { if (this.LastStatusDate.CompareTo(to.LastStatusDate)<0) return 1; else return 0; }
public int CompareTo(Issue to) { if (this.LastStatusDate > to.LastStatusDate) return 1; else return 0; }I also tried DateTime.Compare(date1,date2). This didn't work either.Any idea why the records in my gridview come up in complete disorder?
Thanks and regards,
Dani
Dani OpreanPosted Sep 6, 2010, 3:33 AM
What i meant was that i have a class which i want to sort by a datetime field. Anyway, i am stuck...i really don't have any other solution right now.
Below i post the code for the 4 ways i tried to implement CompareTo and the rest of the code, up to the point where i bind the list to the gridview.
This is the CompareTo method:
public int CompareTo(Issue to)
{
//VER 1
//if (this.LastStatusDate.Year > to.LastStatusDate.Year) return 1;
//if ((this.LastStatusDate.Year == to.LastStatusDate.Year) &&
// (this.LastStatusDate.Month > to.LastStatusDate.Month)) return 1;
//if ((this.LastStatusDate.Year == to.LastStatusDate.Year) &&
// (this.LastStatusDate.Month == to.LastStatusDate.Month) &&
// (this.LastStatusDate.Day > to.LastStatusDate.Day)) return 1;
//if ((this.LastStatusDate.Year == to.LastStatusDate.Year) &&
// (this.LastStatusDate.Month == to.LastStatusDate.Month) &&
// (this.LastStatusDate.Day == to.LastStatusDate.Day) &&
// (this.LastStatusDate.Hour > to.LastStatusDate.Hour)) return 1;
//return 0;
//VER 2
//if (this.LastStatusDate.CompareTo(to.LastStatusDate)<0) return 1;
//else return 0;
//VER 3
//if (DateTime.Compare(this.LastStatusDate, to.LastStatusDate)<0) return 1;
//else return 0;
//VER 4
if (this.LastStatusDate > to.LastStatusDate) return 1;
else return 0;
}
Now the code in the aspx.cs, where i get the data, sort it and bind it:
if (!IsPostBack))IssueDao.searchIssues("JobName", "");
{
AllIssuesList = (List
AllIssuesList.Sort();
AllIssuesList.Reverse();
grdIssues.DataSource = AllIssuesList;
grdIssues.DataBind();
}
And the code for the searchIssues method (i use nhibernate):
public static IList searchIssues(string searchColumn, string searchText) list = null;(query.List());
{
ISession session = NHibernateSessionFactory.OpenSession();
IList
try
{
IQuery query = null;
query = session.CreateQuery("select new Issue(I.IssueId,I.AbendCode,I.JobName,I.IssueDescription,I.Solution," +
"I.RootCause,I.ResolutionDelays,I.RootCauseCategory,I.IssueCategory,I.BusinessImpact," +
"I.NotifiedUser,I.Status,I.ReportedBy,I.ContendingSource,I.Time,I.OVSDTicketId,max(IL.LogTime))"+
" from Issue I,IssueLog IL where IL.IssueId=I.IssueId "+
"group by I.IssueId,I.AbendCode,I.JobName,I.IssueDescription,I.Solution,I.RootCause,I.ResolutionDelays,"+
"I.RootCauseCategory,I.IssueCategory,I.BusinessImpact,I.NotifiedUser,I.Status,I.ReportedBy,"+
"I.ContendingSource,I.Time,I.OVSDTicketId ");
list = Useful.ConvertToGenericList
}
catch (Exception ex)
{
Useful.setError("Error occured while getting issues ",ex);
}
session.Close();
return list;
}
The result of all this is a total mess. No order whatsoever.
P.S. Sam, sorry my english isn't perfect. Not everybody on this forum comes from english speaking countries.
Sam HobbsPosted Sep 3, 2010, 4:38 PM
As Pasan Ratnayake indicates, you should not need to implement a compare for DateTime. So you need to be methodical about diagnosing the problem. Ensure that the compare works; whatever you are usng for the compare. Don't assume that the compare is not working just because the data in the grid is not sorted. It seems likely that the compare is working but something else is not but you are assuming that the problem is the compare.
Pasan RatnayakePosted Sep 3, 2010, 12:49 PM
Whatever that is causing your sorting to malfunction is not a problem in the "DateTime" class but somewhere else in your logic. I recommend you check your code again; may be just run through it in debug mode.
Regards