Sort C # ArrayList with DateTime

I have a C # ArrayList with objects that include the following fields. I have about 10 objects in an ArrayList . My problem is that I need to sort an ArrayList based on the StartDate available on 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 .Sort() , but it just doesnโ€™t do anything (dump it to try sorting by ArrayList , which has objects that I know).

Thanks in advance.

Update:

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

I created a List as above. Can I sort it now?

+4
source share
2 answers

Try to create a mapping as shown 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 ()); 

OR

Create a shared collection than you can do it

try Enumerable.OrderBy work will work for you .. you need to enable linq for this

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

if the list is created as shown below than the linq variant

 List<MYCLASS> arrayList = new List<MYCLASS>(); 
+6
source

You should try to make this property set into one class, and then create a common List<NewClass> , and then you can use IComparable and IComparer to sort by a specific field.

+1
source

All Articles