using System;
using System.Collections;public class ReverserCompare : IComparer
{
int IComparer.Compare(object x, object y)
{
return new CaseInsensitiveComparer().Compare(x, y);
}
}class Program
{
static void Main()
{
ArrayList a = formatStringTotal("14|15|11|11|10|20|11|12|12|9|9|12|16|13|10|5|14|21|17|12|24|11|10|17|21|17|22|24|16|16|9|20|7|13|9|10|5|23|17|10|15|14|10|9|17|7|19|15|9|6|17|11|15|19|12|18|16|15|9|7|13|16|19|8|15");
//想要得结果是不重复,按顺序排列显示,正序和反序都可以,偏偏输出这样一个结果: 9,8,7,6,5,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10 或10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,5,6,7,8,9
a.Sort(new ReverserCompare());
foreach (string x in a)
{
Console.WriteLine(x);
}
Console.ReadKey();
} static ArrayList formatStringTotal(String st)
{
String[] k = st.Split('|');
ArrayList al = new ArrayList();
for (int i = 0; i < k.Length; i++)
{
if (!al.Contains(k[i]))
{
al.Add(k[i].Trim());
}
}
return al;
}
}
using System.Collections;public class ReverserCompare : IComparer
{
int IComparer.Compare(object x, object y)
{
return new CaseInsensitiveComparer().Compare(x, y);
}
}class Program
{
static void Main()
{
ArrayList a = formatStringTotal("14|15|11|11|10|20|11|12|12|9|9|12|16|13|10|5|14|21|17|12|24|11|10|17|21|17|22|24|16|16|9|20|7|13|9|10|5|23|17|10|15|14|10|9|17|7|19|15|9|6|17|11|15|19|12|18|16|15|9|7|13|16|19|8|15");
//想要得结果是不重复,按顺序排列显示,正序和反序都可以,偏偏输出这样一个结果: 9,8,7,6,5,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10 或10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,5,6,7,8,9
a.Sort(new ReverserCompare());
foreach (string x in a)
{
Console.WriteLine(x);
}
Console.ReadKey();
} static ArrayList formatStringTotal(String st)
{
String[] k = st.Split('|');
ArrayList al = new ArrayList();
for (int i = 0; i < k.Length; i++)
{
if (!al.Contains(k[i]))
{
al.Add(k[i].Trim());
}
}
return al;
}
}
using System.Collections.Generic;class Program
{
static void Main()
{
string st = "14|15|11|11|10|20|11|12|12|9|9|12|16|13|10|5|14|21|17|12|24|11|10|17|21|17|22|24|16|16|9|20|7|13|9|10|5|23|17|10|15|14|10|9|17|7|19|15|9|6|17|11|15|19|12|18|16|15|9|7|13|16|19|8|15";
int[] t = Sort(st);
foreach (int x in t)
{
Console.WriteLine(x);
}
}
static int[] Sort(string s)
{
List<int> list = new List<int>();
foreach (string t in s.Split('|'))
{
int x = int.Parse(t);
if (!list.Contains(x)) list.Add(x);
}
list.Sort();
return list.ToArray();
}
}
/* 输出:
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
*/
using System.Collections.Generic;class Program
{
static void Main()
{
string st = "14|15|11|11|10|20|11|12|12|9|9|12|16|13|10|5|14|21|17|12|24|11|10|17|21|17|22|24|16|16|9|20|7|13|9|10|5|23|17|10|15|14|10|9|17|7|19|15|9|6|17|11|15|19|12|18|16|15|9|7|13|16|19|8|15";
string t = Sort(st);
Console.WriteLine(t); // 输出:5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24
}
static string Sort(string s)
{
List<int> list = new List<int>();
foreach (string t in s.Split('|'))
{
int x = int.Parse(t);
if (!list.Contains(x)) list.Add(x);
}
list.Sort();
string[] a = new string[list.Count];
for (int i = 0; i < a.Length; i++)
{
a[i] = list[i].ToString();
}
return string.Join(",", a);
}
}
using System.Collections.Generic;class Program
{
static void Main()
{
string st = "14|15|11|11|10|20|11|12|12|9|9|12|16|13|10|5|14|21|17|12|24|11|10|17|21|17|22|24|16|16|9|20|7|13|9|10|5|23|17|10|15|14|10|9|17|7|19|15|9|6|17|11|15|19|12|18|16|15|9|7|13|16|19|8|15";
string[] t = Sort(st);
foreach (string x in t)
{
Console.WriteLine(x);
}
Console.ReadKey();
} static string[] Sort(string s)
{
List<string> list = new List<string>();
foreach (string t in s.Split('|'))
{
if (!list.Contains(t)) list.Add(t);
}
list.Sort();
return list.ToArray();
}
}这样就不行了,因为那些元素可能是字符串的,所以不能用int
using System.Collections.Generic;class Program
{
static void Main()
{
string st = "abcd|14|xyz|15|11|11|10|20|11|12|12|9|9|12|16|13|10|5|14|21|17|12|24|11|10|17|21|17|22|24|16|16|9|20|7|13|9|10|5|23|17|10|15|14|10|9|17|7|19|15|9|6|17|11|15|19|12|18|16|15|9|7|13|16|19|8|15";
string t = Sort(st);
Console.WriteLine(t); // 输出:5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24
}
static string Sort(string s)
{
List<int> list = new List<int>();
foreach (string t in s.Split('|'))
{
int x;
if (int.TryParse(t, out x) && !list.Contains(x)) list.Add(x);
}
list.Sort();
string[] a = new string[list.Count];
for (int i = 0; i < a.Length; i++)
{
a[i] = list[i].ToString();
}
return string.Join(",", a);
}
}
想要aa,cc,dd,ee这样的结果怎么弄?
output results like:
aa,cc,dd,ee
如果不需要按整数排序,就用的方法,不要转换为 int。
00,99,aa,bb,dd,ff
using System.Collections.Generic;class Program
{
const int maxWidth = 20; // 字符串可能的最大宽度
static void Main()
{
string st = "Z|14|xy|15|11|11|10|20|11|12|12|9|9|12|16|13|10|5|14|21|17|12|24|11|10|17|21|17|22|24|16|16|9|20|7|13|9|10|5|23|17|10|15|14|10|9|17|7|19|15|9|6|17|11|15|19|12|18|16|15|9|7|13|16|19|8|15";
string t = Sort(st);
Console.WriteLine(t); // 输出:5,6,7,8,9,Z,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,xy
}
static string Sort(string s)
{
List<string> list = new List<string>();
foreach (string t in s.Split('|'))
{
string x = t.PadLeft(maxWidth);
if (!list.Contains(x)) list.Add(x);
}
list.Sort();
string[] a = new string[list.Count];
for (int i = 0; i < a.Length; i++)
{
a[i] = list[i].TrimStart();
}
return string.Join(",", a);
}
}
试试这样
在下面代码里将字符串转换成int数组再排序应该就OK了
我以前写个一个自动排序的类的
现在没工具在身边,没多少时间,就说个思路给你吧
1.先判断objece A 和object B
如果都是数字(用try double.Parse(A.ToString())和double.Parse(B.ToString())
如果捕抓到异常,就按照字符串来比较,如果顺利转换,就按浮点类型比较(int类也可以按照浮点类比较))
现在就可以写分类比较了
再用转换后的类型比较a.CompareTo(b)
如果比较的类型是你自己写的类,内含多种基本类型,就按照一下办法解决
写两个内部类,都实现ICompare接口
一个用来比较double,一个用来比较string
上面的思路也可以用来分类
用内部类返回的接口比较(用array.sort(内部类返回接口,对象)排序)
有空我找到那个我写的类代码
贴出来给你参考参考我也是才学了半年多的,假如思路有问题,还请包涵指点
如果是包含数字和字符串混杂类型的排序的话
按照我上面说的思路
我已经成功实现了,在一个DataGridView继承类中
改天把代码考来给你参考参考
贴出实现代码给你
呵呵
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;namespace 更新触发试验
{
class coo
{
static A _a = null;
public static Array sorted(Array aaa)
{
if (_a == null)
_a = new A();
Array.Sort(aaa,((IComparer)_a));
return aaa;
}
//创建内部静态类,用于实现比较接口
private class A:IComparer
{
#region IComparer 成员 public int Compare(object x, object y)
{
double? i = null;
double? j = null;
try
{
i = Convert.ToDouble(x);
}
catch
{
i = null;
}
try
{
j = Convert.ToDouble(y);
}
catch
{
j = null;
}
//两者都是字符串
if (i == null && j == null)
return x.ToString().CompareTo(y.ToString());
//根据你想把数字排在字符串前还是后定义返回值
else if (i == null && j != null)
return 1;
//这个和上面相反
else if (i != null && j == null)
return -1;
else
return i.Value.CompareTo(j.Value);
} #endregion
}
}
}使用的时候,引用这个类,在另一个类中的方法内使用
ArrayList al = new ArrayList(10);
//添加集合成员
al.Add(20);
al.Add("sef");
al.Add(5);
al.Add(55);
al.Add("s12");
al.Add("123");
al.Add("02sas");
al.Add("s59");
al.Add(109);
al.Add("qq106");
//将集合链表转换成集合
Array ao = al.ToArray();
//调用上面的排序方法
coo.sorted(ao);
//将集合绑定到ComboBox看看结果(WinForm)
comboBox1.DataSource = ao;
这个算法可以方便地排序ListView
我已经用这个算法实现了listview的按随意列排序 private static AAA aaa = null;
private static BBB bbb = null; private class AAA : IComparer
{
//用来获取二维表的列
public int columnaa = 0;
public int Compare(object x, object y)
{
//只用于比较ListView对象
if (x is ListViewItem&&y is ListViewItem)
{
//用来存放转换后的x的浮点数容器
double? xd=null;
//用来存放转换后的y的浮点数容器
double? yd = null;
//用来存放转换后的x的字符串容器
string xs = null;
//用来存放转换后的y的字符串容器
string ys = null;
//分别转换x和y
try
{
xd = double.Parse(((ListViewItem)x).SubItems[columnaa].Text);
}
catch
{
//转换失败后将浮点数容器置空,同时将字符串容器赋值
xd = null;
xs = ((ListViewItem)x).SubItems[columnaa].Text;
}
try
{
yd = double.Parse(((ListViewItem)y).SubItems[columnaa].Text);
}
catch
{
//转换失败后将浮点数容器置空,同时将字符串容器赋值
yd = null;
ys = ((ListViewItem)y).SubItems[columnaa].Text;
}
//两者转换失败,说明两个都是字符串
if (xd == null && yd == null)
return xs.CompareTo(ys);
//x是字符串而y是数字,返回值根据你想把数字排在字符串前还是后
else if (xd == null && yd != null)
return 1;
//这个情形和上面相反
else if (xd != null && yd == null)
return -1;
//都转换成功,则比较两个浮点数
else
return ((double)xd).CompareTo((double)yd);
}
//如果不是ListView的对象,抛出异常
else
throw new Exception("对象必须为ListViewItem类型且不为空!");
}
}
private class BBB : IComparer
{
//用来获取二维表的列
public int columnbb = 0;
public int Compare(object x, object y)
{
//这种情况用于比较两个对象都是字符串
if (x is ListViewItem&&y is ListViewItem)
{
return (((ListViewItem)x).SubItems[columnbb].Text).CompareTo(((ListViewItem)y).SubItems[columnbb].Text);
}
else
throw new Exception("对象必须为ListViewItem类型且不为空!");
}
}