Jay Mayu Jay Mayu - 1 year ago 101
C# Question

Sort C# ArrayList with DateTime

I have a C#

with objects that includes following fields. I have around 10 objects in the
. My problem is I need to sort the
based on the
available in the system which is a DateTime variable.

public int id { get; set; }
public string title {get; set;}
public bool allDay { get; set; }
public string start { get; set; }
public string end { get; set; }
public string color { get; set; }
public DateTime startDate { get; set; }
public DateTime endDate { get; set; }
public string Location { get; set; }
public string StartDatestr { get; set; }
public string EndDatestr { get; set; }
public string StartTime { get; set; }
public string EndTime { get; set; }
public bool Alert { get; set; }
public bool Repeat { get; set; }
public Int32 RepeatDays { get; set; }
public Int32 CalendarID { get; set; }
public int CustomerNo { get; set; }
public string CustomerName { get; set; }
public bool IsProspect { get; set; }
public string Description { get; set; }

Can someone please enlighten me? I tried
but it just doesn’t do anything (its dump to try sort on
which has objects I know).

Thanks in advance.


List<Appointment> appointments = new List<Appointment>();

I created the
as above. Now can I sort it?

Answer Source

Try by creating comparere like as below

public class ComparerDateTime : IComparer
    public int Compare(object x, object y)
        MYCLASS X = x as MYCLASS;
        MYCLASS Y = y as MYCLASS;

        return X.date.CompareTo(Y.date);

 MYCLASSList.Sort(new ComparerDateTime ());


Create Generic collection than you can do this

give try to Enumerable.OrderBy operation will do work for you..you need to include linq for this

IEnumerable<Pet> query = arrayList .OrderBy(x=> x.startDate );

if list is created like as below than try linq option

List<MYCLASS> arrayList  = new List<MYCLASS>();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download