关于集合排序icomparer接口

liuyilin999 2010-09-29 09:29:43
关于集合排序icomparer接口

'实现了icomparer接口
Public Class Comparer
Implements IComparer
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
Dim CC As New CaseInsensitiveComparer()
Return CC.Compare(y, x)
End Function
End Class

'排序
ArrayList.Sort(new Comparer)



请问为什么 ArrayList.Sort(new Comparer)会自己去执行Compare(x, y)方法
并没有调用里面的方法Comparer.Compare(x, y)

谢谢
...全文
192 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
liuyilin999 2010-09-30
  • 打赏
  • 举报
回复
谢谢bclz_vs
给分
liuyilin999 2010-09-29
  • 打赏
  • 举报
回复
请问为什么 ArrayList.Sort(new Comparer)会自己去执行Compare(x, y)方法
并没有调用里面的方法Comparer.Compare(x, y)
边城的刀声 2010-09-29
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 liuyilin999 的回复:]

引用 8 楼 bclz_vs 的回复:
你这个叫显式接口实现,是为了隐藏接口中的成员用的,一般情况下不推荐使用显式接口实现

看来没有明白我的意思
ArrayList.Sort(new Comparer)是怎样执行Comparer类中方法的?并传入A,Y
[/Quote]

internal void QuickSort(int left, int right)
{
do
{
int low = left;
int hi = right;
int median = Array.GetMedian(low, hi);
this.SwapIfGreaterWithItems(low, median);
this.SwapIfGreaterWithItems(low, hi);
this.SwapIfGreaterWithItems(median, hi);
object y = this.keys[median];
do
{
try
{
while (this.comparer.Compare(this.keys[low], y) < 0)
{
low++;
}
while (this.comparer.Compare(y, this.keys[hi]) < 0)
{
hi--;
}
}
catch (IndexOutOfRangeException)
{
throw new ArgumentException(Environment.GetResourceString("Arg_BogusIComparer", new object[] { y, y.GetType().Name, this.comparer }));
}
catch (Exception exception)
{
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"), exception);
}
catch
{
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"));
}
if (low > hi)
{
break;
}
if (low < hi)
{
object obj3 = this.keys[low];
this.keys[low] = this.keys[hi];
this.keys[hi] = obj3;
if (this.items != null)
{
object obj4 = this.items[low];
this.items[low] = this.items[hi];
this.items[hi] = obj4;
}
}
low++;
hi--;
}
while (low <= hi);
if ((hi - left) <= (right - low))
{
if (left < hi)
{
this.QuickSort(left, hi);
}
left = low;
}
else
{
if (low < right)
{
this.QuickSort(low, right);
}
right = hi;
}
}
while (left < right);
}


这是部分源码,感兴趣的话,楼主自己用Reflector反编译去研究吧
兔子-顾问 2010-09-29
  • 打赏
  • 举报
回复
Sort会自动调用
liuyilin999 2010-09-29
  • 打赏
  • 举报
回复
不能沉呀
liuyilin999 2010-09-29
  • 打赏
  • 举报
回复
写错了
ArrayList.Sort(new Comparer)是怎样执行Comparer类中方法的(我并没有调用这个方法)?并传入X,Y
liuyilin999 2010-09-29
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 bclz_vs 的回复:]
你这个叫显式接口实现,是为了隐藏接口中的成员用的,一般情况下不推荐使用显式接口实现
[/Quote]
看来没有明白我的意思
ArrayList.Sort(new Comparer)是怎样执行Comparer类中方法的?并传入A,Y
边城的刀声 2010-09-29
  • 打赏
  • 举报
回复
《.net设计规范》第二版中对这个介绍的比较详细
边城的刀声 2010-09-29
  • 打赏
  • 举报
回复
你这个叫显式接口实现,是为了隐藏接口中的成员用的,一般情况下不推荐使用显式接口实现
liuyilin999 2010-09-29
  • 打赏
  • 举报
回复
请问为什么 ArrayList.Sort(new Comparer)会自己去执行Compare(x, y)方法
并没有调用里面的方法Comparer.Compare(x, y)
liuyilin999 2010-09-29
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 liuyilin888 的回复:]
明白楼主的意思了
是说:只声明了接口的类new Comparer,没调用里面的方法Compare(x, y),但实际里面的方法被执行了,为什么?
等高人
[/Quote]
是这个意思,大吓解释下,
liuyilin888 2010-09-29
  • 打赏
  • 举报
回复
明白楼主的意思了
是说:只声明了接口的类new Comparer,没调用里面的方法Compare(x, y),但实际里面的方法被执行了,为什么?
等高人
liuyilin888 2010-09-29
  • 打赏
  • 举报
回复
是不是Sort(icomparer)中有代码

16,722

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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