使用BinarySearch时,如何只查找其中的某一个部分,而忽略其它部分。
public class ItemArray: IComparable
{
public int s_ID;
public int s_LEN;
public bool s_BOOL;
……
}
如果只比较s_LEN,而忽略其它部分:s_DI和s_BOOL,应该怎么办?
if(als.BinarySearch(new ItemArray(i,CateID.Length,true))<0)
解决方案 »
- asp.net html控件asp控件之间的关系
- DataGridView下拉式框选择后无数据显示
- listBox1_SelectedIndexChanged 发生后...
- C#如何得到C++dll文件返回的字符串
- 帮忙解释下哦! if (someCondition && (++i < 100))
- 一个有意思的话题大家都进来看看!~
- 关于dropdownlist控件和asp:Calendar控件! c#
- VS2005用户控件传递参数问题!
- 求教Repeater实现分页的最佳方法!答对就给分!
- 做网络通讯,怎么设计数据安全解决方案?
- 数据库连接超时值
- 使用存储过程时出现的问题:使用存储过程时超时时间已到。在操作完成之前超时时间已过或服务器未响应。
if(als.BinarySearch(new ItemArray(i,CateID.Length,true), new ItemArrayCompareByLen())<0)
// --------------------
public class ItemArrayCompareByLen: IComparable
{
public int Compare(object obj1, object obj2)
{
//Do compare only by Len
ItemArray item1 = obj1 as ItemArray;
ItemArray item2 = obj2 as ItemArray;
if(item1 != null && item2 != null)
{
return item1.s_LEN - item2.s_LEN;
} throw new NotSupportedException("Only can compare two ItemArray");
}
}
public int s_LEN;
public bool s_BOOL;
……
如果有10个参数,
只有一个参数不需要比较,如比id号,而其它的参数均需要比较,怎么办?
// --------------------
public class ItemArrayCompareByLen: IComparable
{
public int Compare(object obj1, object obj2)
{
//Do compare only by Len
ItemArray item1 = obj1 as ItemArray;
ItemArray item2 = obj2 as ItemArray;
//把下面的比较改成你的比较就可以了,拜托不要让我写现成的,这个都一样,再写一遍挺无聊的。
//if(item1 != null && item2 != null)
//{
// return item1.s_LEN - item2.s_LEN;
//} throw new NotSupportedException("Only can compare two ItemArray");
}
}
lalac这个无法实现比较多项呀。
// --------------------
public class ItemArrayCompareByLen: IComparable
{
public int Compare(object obj1, object obj2)
{
ItemArray item1 = obj1 as ItemArray;
ItemArray item2 = obj2 as ItemArray; // Compare by s_LEN and s_BOOL, suppose s_BOOL is prefected comparing params.
if(item1 != null && item2 != null)
{
int lenDiff = item1.s_LEN - item2.s_LEN;
if(item1.s_BOOL != item2.s_BOOL)
{
// false compare to true, return negative.
// true compare to false, return positive.
return item1.s_BOOL ? (-Math.Abs(lenDiff)) : Math.Abs(lenDiff);
}
else
{
return item1.s_LEN - item2.s_LEN;
}
} throw new NotSupportedException("Only can compare two ItemArray");
}
}
return item1.s_BOOL ? (-Math.Abs(lenDiff)) : Math.Abs(lenDiff);
应该是
return item1.s_BOOL ? Math.Abs(lenDiff) : (-Math.Abs(lenDiff));
{
enum CompareItem
{
ID,
Len,
Bool
}
private CompareItem m_CompareItem = CompareItem.ID;
public int s_ID;
public int s_LEN;
public bool s_BOOL;
public ItemArray(CompareItem compareItem)
{
m_CompareItem = compareItem;
}
#region IComparable メンバ public int CompareTo( object obj )
{
if( obj is ItemArray )
{
if( m_CompareItem == CompareItem.ID )
{
}
else if( m_CompareItem == CompareItem.Len )
{
}
else if( m_CompareItem == CompareItem.Bool )
{
}
}
else
{
throw new Exception( "Don't Comparable!" );
}
} #endregion
}
这样就可以根据不同的参数进行比较了吧。
请看另外一贴,这就是个实例了。
http://topic.csdn.net/u/20080107/05/27e0eaf2-829b-4aac-b836-f67e2c67477e.html?seed=1214725616