注意,只能用:IComparable。谢谢先。private string View()
{
string msg = null;
ArrayList al = new ArrayList();
al.Add(new ArrayItem(1,0,true));
al.Add(new ArrayItem(2,0,true));
al.Add(new ArrayItem(3,0,true));
al.Add(new ArrayItem(4,0,true));
al.Add(new ArrayItem(1,1,true));
al.Add(new ArrayItem(2,1,true));
al.Add(new ArrayItem(3,1,true));
al.Add(new ArrayItem(4,1,true));
ArrayItem.SortOrder = SortItem.s_layer_ASC;
int f1 = al.BinarySearch(new ArrayItem(1,5,true));
ArrayItem.SortOrder = SortItem.s_item_ASC;
int f2 = al.BinarySearch(new ArrayItem(5,1,true));
msg += f1;
msg += " 和 ";
msg += f2;
//不存在的这两个搜索参数,搜索到的结果竟然为:0 和 5 !!!
return msg;
}
public class ArrayItem: IComparable
{
public static SortItem SortOrder = SortItem.s_layer_ASC;
public int s_Layer;
public int s_Item;
public bool s_LastReco;
public int s_layer
{
get
{
return s_Layer;
}
}
public int s_item
{
get
{
return s_Item;
}
}
public bool s_lastreco
{
get
{
return s_LastReco;
}
}
public ArrayItem(int _layer, int _item, bool _lastreco)
{
this.s_Layer = _layer;
this.s_Item = _item;
this.s_LastReco = _lastreco;
}
public int CompareTo(object obj)
{
ArrayItem s = (ArrayItem)obj;
switch(SortOrder)
{
case SortItem.s_item_DESC:
return s.s_item.CompareTo(this.s_Item);
case SortItem.s_item_ASC:
return this.s_Item.CompareTo(s.s_item);
case SortItem.s_layer_DESC:
return s.s_layer.CompareTo(this.s_Layer);
default:
return this.s_Layer.CompareTo(s.s_layer);
}
}
private SortItem _sortItem;
public ArrayItem(SortItem sortItem)
{
_sortItem = sortItem;
}
public ArrayItem()
{
_sortItem = SortItem.s_layer_ASC;
}
}
public enum SortItem
{
s_layer_ASC,s_layer_DESC,
s_item_ASC,s_item_DESC
};
{
string msg = null;
ArrayList al = new ArrayList();
al.Add(new ArrayItem(1,0,true));
al.Add(new ArrayItem(2,0,true));
al.Add(new ArrayItem(3,0,true));
al.Add(new ArrayItem(4,0,true));
al.Add(new ArrayItem(1,1,true));
al.Add(new ArrayItem(2,1,true));
al.Add(new ArrayItem(3,1,true));
al.Add(new ArrayItem(4,1,true));
ArrayItem.SortOrder = SortItem.s_layer_ASC;
int f1 = al.BinarySearch(new ArrayItem(1,5,true));
ArrayItem.SortOrder = SortItem.s_item_ASC;
int f2 = al.BinarySearch(new ArrayItem(5,1,true));
msg += f1;
msg += " 和 ";
msg += f2;
//不存在的这两个搜索参数,搜索到的结果竟然为:0 和 5 !!!
return msg;
}
public class ArrayItem: IComparable
{
public static SortItem SortOrder = SortItem.s_layer_ASC;
public int s_Layer;
public int s_Item;
public bool s_LastReco;
public int s_layer
{
get
{
return s_Layer;
}
}
public int s_item
{
get
{
return s_Item;
}
}
public bool s_lastreco
{
get
{
return s_LastReco;
}
}
public ArrayItem(int _layer, int _item, bool _lastreco)
{
this.s_Layer = _layer;
this.s_Item = _item;
this.s_LastReco = _lastreco;
}
public int CompareTo(object obj)
{
ArrayItem s = (ArrayItem)obj;
switch(SortOrder)
{
case SortItem.s_item_DESC:
return s.s_item.CompareTo(this.s_Item);
case SortItem.s_item_ASC:
return this.s_Item.CompareTo(s.s_item);
case SortItem.s_layer_DESC:
return s.s_layer.CompareTo(this.s_Layer);
default:
return this.s_Layer.CompareTo(s.s_layer);
}
}
private SortItem _sortItem;
public ArrayItem(SortItem sortItem)
{
_sortItem = sortItem;
}
public ArrayItem()
{
_sortItem = SortItem.s_layer_ASC;
}
}
public enum SortItem
{
s_layer_ASC,s_layer_DESC,
s_item_ASC,s_item_DESC
};
解决方案 »
- socket收包会出现这种情况吗
- 图像的平移
- winfrom 上传图片asp.net使用解决方案
- 求用C# 通过TCP协议对 网络转串口设备发送数据...
- 求助:在遍历文件夹时遇到System Volume Information系统会提示无法访问,从而中断遍历,如何访问该文件夹或者跳过???
- 正则表达式问题 大家来看看了!
- 如何让客户端IE显示XPS文档?需要时让其下载插件之类的东西, 这种效果怎样实现?
- c#如何实现随着鼠标移动的文字
- 最近有很多的朋友提供了C#的Ftp,能否把它总结一下。共同为了C#的明天奋斗!!!!!!!
- 什么型号的手机可以用来编程?
- 小问题一个,高手花20秒
- 用vs2008写的软件,能用vs2005调试吗?怎么调试?
ArrayItem.SortOrder = SortItem.s_layer_ASC;
int f1 = al.BinarySearch(new ArrayItem(1,5,true));
//f1=0是正確的啊,匹配 al.Add(new ArrayItem(1,0,true));。2.
ArrayItem.SortOrder = SortItem.s_item_ASC;
int f2 = al.BinarySearch(new ArrayItem(5,1,true));
//f2=5就有些奇怪了,如果按照原來的順序查找,應該f2=4。3.奇怪的現象,改變列表的值,測試結果如下:
當
al.Add(new ArrayItem(1,0,true));//
al.Add(new ArrayItem(2,0,true));//
al.Add(new ArrayItem(3,0,true));//
al.Add(new ArrayItem(4,0,true));//
al.Add(new ArrayItem(1,0,true));//
al.Add(new ArrayItem(2,1,true));//
al.Add(new ArrayItem(3,1,true));//
al.Add(new ArrayItem(4,1,true));//
//結果f2=5 //正常,但是和沒修改之前的結果也是一樣的。al.Add(new ArrayItem(1,0,true));//
al.Add(new ArrayItem(2,0,true));//
al.Add(new ArrayItem(3,0,true));//
al.Add(new ArrayItem(4,0,true));//
al.Add(new ArrayItem(1,0,true));//
al.Add(new ArrayItem(2,0,true));//
al.Add(new ArrayItem(3,1,true));//
al.Add(new ArrayItem(4,1,true));//
//結果f2=6 //正常al.Add(new ArrayItem(1,0,true));//
al.Add(new ArrayItem(2,0,true));//
al.Add(new ArrayItem(3,0,true));//
al.Add(new ArrayItem(4,0,true));//
al.Add(new ArrayItem(1,0,true));//
al.Add(new ArrayItem(2,0,true));//
al.Add(new ArrayItem(3,0,true));//
al.Add(new ArrayItem(4,1,true));//
//結果f2=7 //正常
所以......
int f1 = al.BinarySearch(new ArrayItem(1,5,true));
ArrayItem.SortOrder = SortItem.s_item_ASC;
int f2 = al.BinarySearch(new ArrayItem(5,1,true));因为这两个搜索在原ArrayList中不存在,这两个搜索结果应该是负数。
{
ArrayItem s = (ArrayItem)obj;
switch (SortOrder)
{
case SortItem.s_item_DESC:
if (s.s_item.CompareTo(this.s_Item) == 0)
//如果只指定item时,默认layer按desc排序
//更好的做法是layer、item都可以指定排序方式。
return s.s_layer.CompareTo(this.s_Layer);
else
return s.s_item.CompareTo(this.s_Item); case SortItem.s_item_ASC:
if (s.s_item.CompareTo(this.s_Item) == 0)
return s.s_layer.CompareTo(this.s_Layer);
else
return this.s_Item.CompareTo(s.s_item);
case SortItem.s_layer_DESC:
if (s.s_layer.CompareTo(this.s_Layer) == 0)
return s.s_item.CompareTo(this.s_Item);
else
return s.s_layer.CompareTo(this.s_Layer);
default:
if (s.s_layer.CompareTo(this.s_Layer) == 0)
return s.s_item.CompareTo(this.s_Item);
else
return this.s_Layer.CompareTo(s.s_layer);
}
}
al.Sort(); //排序都没排好,怎么能BinarySearch()呢。
int f1 = al.BinarySearch(new ArrayItem(1,5,true));
{
ByLayer_Desc = 1,
ByLayer_Asc = 2,
ByItem_Desc = 4,
ByItem_Asc = 8,
ByOther_Desc = 16,
ByOther_Asc = 32
}用2的幂来定义,这样可以支持ByLayer_Desc | ByItem_Asc,具体的实现自己去做做看。挺有技巧的。
这样就可以取消多个if了。
private string View()
{
string msg = null;
ArrayList al = new ArrayList();
al.Add( new ArrayItem( 6, 0, true ) );
al.Add( new ArrayItem( 2, 0, true ) );
al.Add( new ArrayItem( 3, 0, true ) );
al.Add( new ArrayItem( 4, 0, true ) );
al.Add( new ArrayItem( 6, 2, true ) );
al.Add( new ArrayItem( 2, 2, true ) );
al.Add( new ArrayItem( 3, 2, true ) );
al.Add( new ArrayItem( 4, 2, true ) );
ArrayItem.SortOrder = SortItem.s_layer_ASC;
int f1 = al.BinarySearch( new ArrayItem( 1, 5, true ) );
ArrayItem.SortOrder = SortItem.s_item_ASC;
int f2 = al.BinarySearch( new ArrayItem( 5, 1, true ) );
msg += f1;
msg += " And ";
msg += f2;
//不存在的这两个搜索参数,搜索到的结果竟然为:0 和 5 !!!
return msg;
}
这样就会得出负值。
建议看看Generics方面的东西。一个List<T>要排序的话,数据部分不需要实现IComparable,比较时传入一个Comparer<T>做参数就好了。不同的排序选择,就传入不同的Comparer<T>,肯定比你这里的实现简单高效。
等下一版改进的时候再用.net 2.0或.net 3.0