注意,只能用: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
};

解决方案 »

  1.   

    binarysearch 是要求原来arraylist里面是有序的。但是类怎么算有序,这个我不使很清楚,还请高人回答
      

  2.   

    1.
    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 //正常
      

  3.   

    你 CompareTo函数的 switch 里面 第一次比较的是1 第二次比较的还是1
    所以......
      

  4.   

        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));因为这两个搜索在原ArrayList中不存在,这两个搜索结果应该是负数。
      

  5.   

    3楼的分析是对的楼主是哪抄来的代码?你完全没懂这段代码的意思吗?ArrayItem.SortOrder   =   SortItem.s_layer_ASC;按layer顺序查找,只找layer符合的项int   f1   =   al.BinarySearch(new   ArrayItem(1,5,true));  这里面只需要查找1,5,true 符合1的就是第一项而ArrayItem.SortOrder   =   SortItem.s_item_ASC; 是按item顺序查找,只找item符合的项 int   f2   =   al.BinarySearch(new   ArrayItem(5,1,true)); 
      

  6.   

    CompareTo有问题。public int CompareTo(object obj)
    {
       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);
       }
    }
      

  7.   

    Efcndi:如果有三个或三个以上的比较参数,那不是要写很多if吗?
      

  8.   

    PS:ArrayItem.SortOrder = SortItem.s_layer_ASC;
    al.Sort(); //排序都没排好,怎么能BinarySearch()呢。
    int f1 = al.BinarySearch(new ArrayItem(1,5,true));
      

  9.   

    一个更好的办法是通过一个枚举来设置。enum SortType
    {
       ByLayer_Desc = 1,
       ByLayer_Asc = 2,
       ByItem_Desc = 4,
       ByItem_Asc = 8,
       ByOther_Desc = 16,
       ByOther_Asc = 32  
    }用2的幂来定义,这样可以支持ByLayer_Desc | ByItem_Asc,具体的实现自己去做做看。挺有技巧的。
    这样就可以取消多个if了。
      

  10.   

    兄弟,问题是你这段程序没有问题,查出来的索引位置是对的啊。
            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;
            }
    这样就会得出负值。
      

  11.   

    这样使用ArrayList简直是效率低下。
    建议看看Generics方面的东西。一个List<T>要排序的话,数据部分不需要实现IComparable,比较时传入一个Comparer<T>做参数就好了。不同的排序选择,就传入不同的Comparer<T>,肯定比你这里的实现简单高效。
      

  12.   

    嘿嘿,我用的是.net 1.1,List<T>,目前还不支持。
    等下一版改进的时候再用.net 2.0或.net 3.0