现在有这几个字符串:
first_1.0.2.1010
first_1.0.1.1020
first_2.0.1.2000
first_2.0.2.1000
second_1.0.1.1001
其中:
first_1<first_2<second_1然后按从大到小的顺序排列,请问在如何完成?

解决方案 »

  1.   

    直接比较
    static void Main(string[] args)
    {
        string[] items = new string[] { "first_1.0.2.1010", "first_1.0.1.1020", "first_2.0.1.2000", "first_2.0.2.1000" };
        Array.Sort(items);
        foreach (string item in items)
        {
            Console.WriteLine(item);
        }
        Console.ReadKey();
    }
      

  2.   

    .......好像描述的很混乱。first_1.0.2.1010这是字符串那first_1是什么。你的大小要求是什么。
    你想拍成什么样,输出“first_1<first_2<second_1”字符串么?要比较字符串表示的数字大小,直接类型转换比较就好。
      

  3.   

    var result=(from a in arr select arr ).OrderBy(a=>a);
    Array.Sort
    List.Sort
    SortedList 
      

  4.   

    third1. < fourth1. ?
      

  5.   

    正确的结果是这样的
    second_1.0.1.1001>first_2.0.2.1000>first_2.0.1.2000>first_1.0.2.1010first_1.0.1.10201L的结果不对
      

  6.   


                string[] items = new string[] { "first_1.0.2.1010", "first_1.0.1.1020", "first_2.0.1.2000", "first_2.0.2.1000","second_1.0.1.1001" };
                Array.Sort(items);
                Array.Reverse(items);
                foreach (string item in items)
                {
                    Console.WriteLine(item);
                }
      

  7.   

    抱歉,我没表达清楚,我只是想说1L的结果和我想得到的结果不一样,没别的意思 - -!
    这个虽然可行,如何 多加一个 fourth_1.0.1.1001;
    结果就会变成
    second_1.0.1.1001
    >fourth_1.0.1.1001
    >first_2.0.2.1000
    >first_2.0.1.2000
    >first_1.0.2.1010
    >first_1.0.1.1020但是我想要的是fourth_1.0.1.1001
    >second_1.0.1.1001
    >first_2.0.2.1000
    >first_2.0.1.2000
    >first_1.0.2.1010
    >first_1.0.1.1020
      

  8.   

    比较时先根据前边的first second third fourth么?那就需要定义一个比较器了
    你的所有字符串满足的规则是什么?
      

  9.   

    就是前面的按 first second third fourth... tenth 比较这样排序,后面的就按数字的大小排序.
      

  10.   

    fourth_1.0.1.1001
    second_1.0.1.1001
    first_2.0.2.1000就是 这些可以分为 fourth ,  _1.0.1.1001;second, _1.0.1.1001;first,_2.0.2.1000这样比较但是前面比较first second third fourth... tenth 我不会- -!
      

  11.   

    不知道你的意思如何的。string[] items = new string[] { "first_1.0.2.1010","fouth_1.0.2.0110", "first_1.0.1.1020", "first_2.0.1.2000", "first_2.0.2.1000", "second_1.0.1.1001" };
    string[] nums = "first,second,third,fouth,fifth,sixth,seventh,eighth,ninth,tenth".Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
    Array.Sort(items, (Comparison<string>)delegate(string a, string b) { return Array.IndexOf(nums, b.Substring(0, b.IndexOf('_'))) - Array.IndexOf(nums, a.Substring(0, a.IndexOf('_'))); });
    foreach (string item in items)
    {
        Console.WriteLine(item);
    }输出fouth_1.0.2.0110
    second_1.0.1.1001
    first_2.0.2.1000
    first_1.0.2.1010
    first_1.0.1.1020
    first_2.0.1.2000
      

  12.   


            public class Comp:IComparer
            {            public int Compare(object x, object y)
                {
                    String sort = "first second third fourth... tenth ";                String s1 = x.ToString();
                    String s2 = y.ToString();
                    String s11 = s1.Substring(0,s1.IndexOf('_'));
                    String s12 = s1.Substring(s1.IndexOf('_') + 1);
                    String s21 = s2.Substring(0, s2.IndexOf('_'));
                    String s22 = s2.Substring(s2.IndexOf('_') + 1);                if (sort.IndexOf(s11) > sort.IndexOf(s21))
                    {
                        return -1;
                    }
                    else if (sort.IndexOf(s11) < sort.IndexOf(s21))
                    {
                        return 1;
                    }
                    else
                    {
                        return String.Compare(s12, s22) * (-1) ;
                    }
                }        }
            static void Main(string[] args)
            {
                string[] items = new string[] { "first_1.0.2.1010", "first_1.0.1.1020", "first_2.0.1.2000", "first_2.0.2.1000", "second_1.0.1.1001", "fourth_1.0.1.1001" };
                Array.Sort(items,new Comp());
                foreach (string item in items)
                {
                    Console.WriteLine(item);
                }
             }
      

  13.   

    你把这个String sort = "first second third fourth... tenth ";
    中的...换成对应的5-9
      

  14.   

    Array.Sort(Array arr, IComparer icp)
    icp“比较器",比较arr中两个元素大小的依据定义一个类,实现public int Compare(object x, object y)方法
    在该方法中根据自己的原则来判断x,y的大小
      

  15.   

    取出_之前的单词,到单词表中找一下。然后用找到的下标做比较。不过如果你需要完整的匹配,还要把_后面的部分拿来做字符串比较,例如:static void Main(string[] args)
    {
        string[] items = new string[] { "first_1.0.2.1010", "fouth_1.0.2.0110", "first_1.0.1.1020", "first_2.0.1.2000", "first_2.0.2.1000", "second_1.0.1.1001" };
        string[] nums = "first,second,third,fouth,fifth,sixth,seventh,eighth,ninth,tenth".Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
        Array.Sort(items, (Comparison<string>)delegate(string a, string b) 
        {
            int n = Array.IndexOf(nums, b.Substring(0, b.IndexOf('_'))) - Array.IndexOf(nums, a.Substring(0, a.IndexOf('_')));
            if (n != 0) return n;
            return 0 - string.Compare(a, b);//顺序排列是string.Compare(a, b),逆序可以*-1,也可以用0-一下。
        });
        foreach (string item in items)
        {
            Console.WriteLine(item);
        }    Console.ReadKey();
    }结果:fouth_1.0.2.0110
    second_1.0.1.1001
    first_2.0.2.1000
    first_2.0.1.2000
    first_1.0.2.1010
    first_1.0.1.1020
      

  16.   


    谢谢,不过你的结果输出的不正确
    这是按你的方法输出的结果
    second_1.0.1.1001
    first_2.0.2.1000
    first_2.0.1.2000
    first_1.0.2.1010
    first_1.0.1.1020
    fouth_1.0.2.0110
      

  17.   

    static void Main(string[] args)
            { string[] items = new string[] { "first_1.0.2.1010", "first_1.0.1.1020", "first_2.0.1.2000", "first_2.0.2.1000", "second_1.0.1.1001", "fourth_1.0.1.1001" };
                Array.Sort(items, new Comp());
                foreach (string item in items)
                {
                    Console.WriteLine(item);
                }            Console.ReadKey();
            }
            public class Comp : IComparer<string>
            {            public int Compare(string x, string y)
                {
                    String sort = "first second third fouth fifth sixth seventh eighth ninth tenth ";                String s1 = x.ToString();
                    String s2 = y.ToString();
                    String s11 = s1.Substring(0, s1.IndexOf('_'));
                    String s12 = s1.Substring(s1.IndexOf('_') + 1);
                    String s21 = s2.Substring(0, s2.IndexOf('_'));
                    String s22 = s2.Substring(s2.IndexOf('_') + 1);                if (sort.IndexOf(s11) > sort.IndexOf(s21))
                    {
                        return -1;
                    }
                    else if (sort.IndexOf(s11) < sort.IndexOf(s21))
                    {
                        return 1;
                    }
                    else
                    {
                        return String.Compare(s12, s22) * (-1);
                    }
                }        }这是15楼调试用的代码
      

  18.   

    string[] items = new string[] { "first_1.0.2.1010", "first_1.0.1.1020", "first_2.0.1.2000", "first_2.0.2.1000", "second_1.0.1.1001", "fourth_1.0.1.1001" };测试结果:
    fourth_1.0.1.1001
    second_1.0.1.1001
    first_2.0.2.1000
    first_2.0.1.2000
    first_1.0.2.1010
    first_1.0.1.1020
      

  19.   

    四 不是fourth么?你写的fouth
      

  20.   


    你把fouth写成了fouth,还怪我啊...