现在有这几个字符串:
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然后按从大到小的顺序排列,请问在如何完成?
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然后按从大到小的顺序排列,请问在如何完成?
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();
}
你想拍成什么样,输出“first_1<first_2<second_1”字符串么?要比较字符串表示的数字大小,直接类型转换比较就好。
Array.Sort
List.Sort
SortedList
second_1.0.1.1001>first_2.0.2.1000>first_2.0.1.2000>first_1.0.2.1010first_1.0.1.10201L的结果不对
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);
}
这个虽然可行,如何 多加一个 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
你的所有字符串满足的规则是什么?
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 我不会- -!
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
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);
}
}
中的...换成对应的5-9
icp“比较器",比较arr中两个元素大小的依据定义一个类,实现public int Compare(object x, object y)方法
在该方法中根据自己的原则来判断x,y的大小
{
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
谢谢,不过你的结果输出的不正确
这是按你的方法输出的结果
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
{ 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楼调试用的代码
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
你把fouth写成了fouth,还怪我啊...