Order of data has changed after sorting with date values in data.

  • Thread starter Thread starter Piruthiv
  • Start date Start date
P

Piruthiv

Guest
Hi,

I have a custom class with some fields (Subject and StartTime) and custom comparer for sorting the time values and stored data in ArrayList collection.

Sample Data in collection:

Subject = App1 and StartTime = 25/11/2019

Subject = App1 and StartTime = 26/11/2019

Subject = App1 and StartTime = 27/11/2019

Subject = App1 and StartTime = 28/11/2019

Subject = App1 and StartTime = 29/11/2019

Subject = App1 and StartTime = 30/11/2019

Subject = App1 and StartTime = 01/12/2019

Subject = App2 and StartTime = 25/11/2019

Subject = App2 and StartTime = 26/11/2019

Subject = App2 and StartTime = 27/11/2019

Subject = App2 and StartTime = 28/11/2019

Subject = App2 and StartTime = 29/11/2019

Subject = App2 and StartTime = 30/11/2019

Subject = App2 and StartTime = 01/12/2019

Subject = App3 and StartTime = 25/11/2019

Subject = App3 and StartTime = 26/11/2019

Subject = App3 and StartTime = 27/11/2019

Subject = App3 and StartTime = 28/11/2019

Subject = App3 and StartTime = 29/11/2019

Subject = App3 and StartTime = 30/11/2019

Subject = App3 and StartTime = 01/12/2019

Problem:

FYI : Date values in collection are from 25/11/2019 to 01/12/2019 (including Saturday and Sunday).

After sorting the date values, times are sorted properly but objects or records order is changed.

Expected result:

Subject = App1 and StartTime = 25/11/2019

Subject = App2 and StartTime = 25/11/2019

Subject = App3 and StartTime = 25/11/2019

Subject = App1 and StartTime = 26/11/2019

Subject = App2 and StartTime = 26/11/2019

Subject = App3 and StartTime = 26/11/2019

Observed result:

Subject = App1 and StartTime = 25/11/2019

Subject = App3 and StartTime = 25/11/2019

Subject = App2 and StartTime = 25/11/2019

Subject = App1 and StartTime = 26/11/2019

Subject = App3 and StartTime = 26/11/2019

Subject = App2 and StartTime = 26/11/2019

//

//

Subject = App3 and StartTime = 28/11/2019

Subject = App2 and StartTime = 28/11/2019

Subject = App1 and StartTime = 28/11/2019

Note: This problem occurs only when collection has Saturday and Sunday values (30/11/2019 and 01/12/2019).

If don’t have that 2 dates in collection, the records are in correct order as expected result after sorting.

Here I have attached sample demo & screenshots for your reference.

Please guide me to have sorted objects in correct order as expected result (with Sat and Sun dates) and let me know if I did anything wrong.


Code:

//main method
ArrayList collection = new ArrayList();
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 7; j++)
{
TestDate t = new TestDate() { Subject = "App" + (i + 1).ToString() };
DateTime date = new DateTime(2019, 11, 25);
t.StartTime = date.AddDays(j);
collection.Add(t);
}
}
StartDateComparer comp = new StartDateComparer();
collection.Sort(comp);


//Custom Comparer
public class StartDateComparer : IComparer
{
#region IComparer Members
public int Compare(object x, object y)
{
TestDate xItem = x as TestDate;
TestDate yItem = y as TestDate;
if (xItem == null && yItem == null)
{
return 0;
}
else if (xItem == null)
{
return -1;
}
else if (yItem == null)
{
return 1;
}
else
{
int c = xItem.StartTime.CompareTo(yItem.StartTime);
if (c == 0)
c = xItem.StartTime.Date.CompareTo(yItem.StartTime.Date);
return c;
}
}
#endregion
}







Thanks,

Continue reading...
 
Back
Top