使用DateTime对C#ArrayList进行排序

Jay*_*ayu 4 c# sorting collections arraylist

我有一个ArrayList包含以下字段的对象的C#.我有大约10个物体ArrayList.我的问题是我需要ArrayList根据StartDate系统中可用的DateTime变量对其进行排序.

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; }
Run Code Online (Sandbox Code Playgroud)

有人可以赐教吗?我试过.Sort()但它只是没有做任何事情(它的转储尝试排序ArrayList我知道的对象).

提前致谢.

更新:

List<Appointment> appointments = new List<Appointment>();
Run Code Online (Sandbox Code Playgroud)

我创建了List如上所述.现在可以对它进行排序吗?

Pra*_*ana 6

尝试创建比较如下所示

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 ());
Run Code Online (Sandbox Code Playgroud)

要么

创建通用集合,而不是这样做

尝试Enumerable.OrderBy操作将为你工作..你需要包括linq为此

IEnumerable<Pet> query = arrayList .OrderBy(x=> x.startDate );
Run Code Online (Sandbox Code Playgroud)

如果列表创建如下,请尝试linq选项

List<MYCLASS> arrayList  = new List<MYCLASS>();
Run Code Online (Sandbox Code Playgroud)