10月23日
10月5日
10月7日
10月12日
10月26日代码排序,10月5日
10月7日
10月12日
10月23日
10月26日都是字符串的,求高人写的代码

解决方案 »

  1.   

    这些数据存储到哪里的?数据库还是内存中。
    数据库里的话,存储的时候对于月和日总是存两位,然后orderby了
    内存中,用sortedlist,自定义comparer就可以了。
      

  2.   

    如果你能把你的日期格式统一下的话那我倒是有个办法。比如你的"10月5日"格式一下成:"10月05日"的话,那样把数据装到一datatable或集合中再排序就行了。
      

  3.   


      public class TestComparer : IComparer<string>
        {
            public int Compare(string x, string y)
            {
                if (x == null)
                {
                    if (y == null)
                    {
                        return 0;
                    }
                    else
                    {
                        return -1;
                    }
                }
                else
                {
                    if (y == null)
                    {
                        return 1;
                    }
                    else
                    {
                        int retval = x.Length.CompareTo(y.Length);//这里根据自己需要再整理一下                    if (retval != 0)
                        {
                            return retval;
                        }
                        else
                        {
                           
                            return x.CompareTo(y);
                        }
                    }
                }
            }
        }使用        List<string> list = new List<string>();// { "10月23日", "10月5日", "10月7日", "10月12日", "10月26日" };
            list.Add("10月23日");
            list.Add("10月5日");
            list.Add("10月7日");
            list.Add("10月12日");
            list.Add("10月26日");        list.Sort(new TestComparer());
      

  4.   

            private void button2_Click(object sender, EventArgs e)
            {
                List<String> listStr = new List<string>();
                listStr.Add("10月23日");
                listStr.Add("10月5日");
                listStr.Add("10月7日");
                listStr.Add("10月12日");
                listStr.Add("10月26日");            listStr.Sort(new Comparison<string>(function));
                
            }        int function(string s1, string s2)
            {
                return DateTime.Compare(Convert.ToDateTime(s1), Convert.ToDateTime(s2));            
            }
      

  5.   

    是从数据库中读取出来之后加载到datalist中吗,那样的话,可以用sql语句,从表中读出,然后 order by Year("数据库中时间字段")desc,Month("数据库中时间字段")asc
      

  6.   


                List<string> list = new List<string>();
                list.Add("10月23日");
                list.Add("10月5日");
                list.Add("10月7日");
                list.Add("10月12日");
                list.Add("10月26日");
                list.Sort(new Comparison<string>(delegate(string a, string b)
                    {
                        DateTime dt1 = DateTime.Parse(a);
                        DateTime dt2 = DateTime.Parse(b);
                        return dt1 < dt2 ? -1 : dt1 > dt2 ? 1 : 0;
                    }));
                foreach (string s in list)
                    Console.WriteLine(s);
    /*
    输出:
    10月5日
    10月7日
    10月12日
    10月23日
    10月26日
    */
      

  7.   

    楼上的方法好,我这里写了一个笨办法:        static void Main(string[] args)
            {
                string[] array = new string[5];
                array[0] = "10月23日";
                array[1] = "10月5日";
                array[2] = "10月7日";
                array[3] = "10月12日";
                array[4] = "10月26日";
                string temp ;
                for (int i = 0; i < 5; i++)
                {
                    int monthi = Convert.ToInt32(array[i].Substring(0, array[i].LastIndexOf('月')));
                    int dayi = Convert.ToInt32(array[i].Substring(array[i].LastIndexOf('月') + 1, array[i].LastIndexOf('日') - array[i].LastIndexOf('月') - 1));
                    for (int j = i + 1; j < 5; j++)
                    {
                        int monthj = Convert.ToInt32(array[j].Substring(0, array[j].LastIndexOf('月')));
                        int dayj = Convert.ToInt32(array[j].Substring(array[j].LastIndexOf('月') + 1, array[j].LastIndexOf('日') - array[j].LastIndexOf('月') - 1));
                        if (monthi > monthj)
                        {
                            temp = array[i];
                            array[i] = array[j];
                            array[j] = temp;
                        }
                        else if (monthi == monthj)
                        {
                            if (dayi < dayj)
                            {
                                temp = array[i];
                                array[i] = array[j];
                                array[j] = temp;
                            }
                        }
                    }
                }
                for (int i = 0; i < 5; i++)
                {
                    Console.WriteLine(array[i]);
                }
                Console.ReadKey();
            }
    //输出为:
    10月26日
    10月23日
    10月12日
    10月7日
    10月5日