gpt4 book ai didi

c# - 使用 DateTime 对 C# ArrayList 进行排序

转载 作者:太空狗 更新时间:2023-10-30 00:42:46 25 4
gpt4 key购买 nike

我有一个 C# ArrayList,其中包含以下字段的对象。我在 ArrayList 中有大约 10 个对象。我的问题是我需要根据系统中可用的 StartDateArrayList 进行排序,这是一个 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; }

谁能教教我?我试过 .Sort() 但它什么也没做(它的转储尝试对 ArrayList 进行排序,它有我知道的对象)。

提前致谢。

更新:

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

我如上所述创建了 List。现在我可以排序了吗?

最佳答案

尝试像下面这样创建比较器

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 ());

创建通用集合比你能做的要多

尝试Enumerable.OrderBy操作将为您工作..您需要为此包含 linq

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

如果像下面这样创建列表而不是尝试 linq 选项

List<MYCLASS> arrayList  = new List<MYCLASS>();

关于c# - 使用 DateTime 对 C# ArrayList 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13796568/

25 4 0