ArrayList 不执行排序,请大家帮忙看看。

unixpeter 2009-03-22 02:05:27
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;
}
}
...全文
252 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
mindsky 2009-05-07
  • 打赏
  • 举报
回复
mark
qiufengilove 2009-03-23
  • 打赏
  • 举报
回复
再给你一个实现复杂一点的排序的核心算法
这个算法可以方便地排序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类型且不为空!");
}
}
qiufengilove 2009-03-23
  • 打赏
  • 举报
回复
来了来了
贴出实现代码给你
呵呵
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;
unixpeter 2009-03-22
  • 打赏
  • 举报
回复
thanks
qiufengilove 2009-03-22
  • 打赏
  • 举报
回复
呵呵
如果是包含数字和字符串混杂类型的排序的话
按照我上面说的思路
我已经成功实现了,在一个DataGridView继承类中
改天把代码考来给你参考参考
qiufengilove 2009-03-22
  • 打赏
  • 举报
回复
楼主你用的是按照字符串顺序排序的方法,当然就是按照1,10,12,3,31,4,9,这样的顺序排序了啊
试试这样
在下面代码里将字符串转换成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(内部类返回接口,对象)排序)
有空我找到那个我写的类代码
贴出来给你参考参考

我也是才学了半年多的,假如思路有问题,还请包涵指点
wuyi8808 2009-03-22
  • 打赏
  • 举报
回复
using System; 
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);
}
}
unixpeter 2009-03-22
  • 打赏
  • 举报
回复
aa|99|00|bb|ff|dd|00|00|aa

想要这样的结果
00,99,aa,bb,dd,ff

wuyi8808 2009-03-22
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 unixpeter 的回复:]
aa|dd|ee|cc|aa|aa|cc


output results like:
aa,cc,dd,ee
[/Quote]

如果不需要按整数排序,就用的方法,不要转换为 int。
unixpeter 2009-03-22
  • 打赏
  • 举报
回复
aa|dd|ee|cc|aa|aa|cc


output results like:
aa,cc,dd,ee


unixpeter 2009-03-22
  • 打赏
  • 举报
回复
"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" 改为 "aa,dd,ee,cc,aa,aa,cc"
想要aa,cc,dd,ee这样的结果怎么弄?
wuyi8808 2009-03-22
  • 打赏
  • 举报
回复
要不然就改用 int.TryParse():

using System; 
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);
}
}
wuyi8808 2009-03-22
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 unixpeter 的回复:]
因为那些元素可能是字符串的,所以不能用int
[/Quote]

请举例说明。
wuyi8808 2009-03-22
  • 打赏
  • 举报
回复
要用 int ,不然排序就不是你期望的。
unixpeter 2009-03-22
  • 打赏
  • 举报
回复
using System;
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
wuyi8808 2009-03-22
  • 打赏
  • 举报
回复
using System; 
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);
}
}
unixpeter 2009-03-22
  • 打赏
  • 举报
回复
好,谢谢
wuyi8808 2009-03-22
  • 打赏
  • 举报
回复
using System; 
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
*/
unixpeter 2009-03-22
  • 打赏
  • 举报
回复



我想要5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24这样的结果

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧