List的Sort自定义排序实例

List<UnRecruitInfo> recreuitList = dic["招聘会"] as List<UnRecruitInfo>;
                //需要对招聘会的列表重新排序
                //排序规则:召开时间>=今天的,进行顺排(从今天到将来),其他的顺序不变(从今天到过去,即过期的)
                DateTime now = DateTime.Parse(DateTime.Now.ToShortDateString()),
                         aTime,
                         bTime;
                recreuitList.Sort(delegate(UnRecruitInfo a, UnRecruitInfo b)
                {//排序开始
                    aTime = DateTime.Parse(a.RecTime.ToShortDateString());
                    bTime = DateTime.Parse(b.RecTime.ToShortDateString());
                    if (a.RecTime >= now && b.RecTime >= now)
                    {//顺排(从小到大)

                        if (aTime < bTime) return -1;
                        else if (aTime > bTime) return 1;
                        else return 0;
                    }
                    else
                    {//倒排(从大到小)
                        if (aTime < bTime) return 1;
                        else if (aTime > bTime) return -1;
                        else return 0;
                    }
                });
                rptZQZP.DataSource = recreuitList;
                rptZQZP.DataBind();

posted @ 2011-05-23 09:54  流失的痕迹  阅读(1782)  评论(0编辑  收藏  举报