首页 新闻 赞助 找找看

C# 日期排序(急)

0
[已解决问题] 解决于 2009-03-21 21:06

有关于使用日期排序(仅C#2.0)

以下是日期序列

2009-03-01

2008-06-12

2009-01-12

2008-06-12

2007-06-14

2008-09-18

2004-01-12

 

我想得到的结果是序列号形式(ex:)

日期                序列号

2009-03-01      6

2008-06-12      3

2009-01-12      5

2008-06-12      3

2007-06-14      2

2008-09-18      4

2004-01-12      1

 

 

真的没有分,非常对不起大家!

chenming的主页 chenming | 初学一级 | 园豆:0
提问于:2009-03-21 12:18
< >
分享
最佳答案
0

        class DateSerial
        {
            public DateTime Date;
            public int Serial;

            public DateSerial(DateTime date)
            {
                Date = date;
                Serial = 0;
            }

            public override string ToString()
            {
                return string.Format("{0} {1}", Date.ToString("yyyy-MM-hh"), Serial);
            }
        }

        static void Sort(List<DateSerial> dateList)
        {
            DateTime minDate = new DateTime(0);

            for (int i = 0; i < dateList.Count; i++)
            {
                DateTime tempDate = DateTime.Parse("2099-1-1");

                for (int j = 0; j < dateList.Count; j++)
                {
                    if (dateList[j].Date > minDate)
                    {
                        dateList[j].Serial++;
                        tempDate = dateList[j].Date < tempDate ? dateList[j].Date : tempDate;
                    }
                }

                minDate = tempDate;
            }
        }

        static void Main(string[] args)
        {
            List<DateSerial> dateList = new List<DateSerial>();

            dateList.Add(new DateSerial(DateTime.Parse("2009-03-01")));
            dateList.Add(new DateSerial(DateTime.Parse("2008-06-12")));
            dateList.Add(new DateSerial(DateTime.Parse("2009-01-12")));
            dateList.Add(new DateSerial(DateTime.Parse("2008-06-12")));
            dateList.Add(new DateSerial(DateTime.Parse("2007-06-14")));
            dateList.Add(new DateSerial(DateTime.Parse("2008-09-18")));
            dateList.Add(new DateSerial(DateTime.Parse("2004-01-12")));

            Sort(dateList);

            foreach (DateSerial d in dateList)
            {
                Console.WriteLine(d);
            }

            Console.ReadLine();
        }

eaglet | 专家六级 |园豆:17139 | 2009-03-21 19:41
其他回答(4)
0

你可以把日期和序号都写在数据库里面嘛

然后用DataGrid绑定,

再显示出来,也是一个办法。

★偏翩☆逍遥 | 园豆:205 (菜鸟二级) | 2009-03-21 14:39
0

不好意思,没看懂

builderman | 园豆:256 (菜鸟二级) | 2009-03-21 14:53
0

楼主的意思是相同的日期序列号是一样的,按照日期重小到大排

known | 园豆:163 (初学一级) | 2009-03-21 16:02
0

你在数据里排好序生成一个ROWMBER ,再把数据绑定到控件上

Luffy Huang | 园豆:25 (初学一级) | 2009-03-21 16:32
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册