c#ArrayList 对象集合 按某字段(属性)排序

时间:2024-08-10 23:04:26

主程序代码

newsCompare newsCompare = new ItemManage.newsCompare();

                    newsList.Sort(newsCompare);

自定义类代码(按照CREATEDATE属性排序)

class newsCompare : IComparer
{
int System.Collections.IComparer.Compare(object x, object y)
{
TJZB_GDNEWS model1 = new TJZB_GDNEWS();
TJZB_GDNEWS newsX = (TJZB_GDNEWS)x;
TJZB_GDNEWS newsY = (TJZB_GDNEWS)y;
if (newsX.CREATEDATE > newsY.CREATEDATE)
return -;
if (newsX.CREATEDATE < newsY.CREATEDATE)
return ;
return ;
}
}