c# – 如何对原位进行排序?

c# – 如何对原位进行排序?,第1张

概述我有一个通用的集合: public Items : Collection<Object>{ protected override void InsertItem(int index, Object item) { base.InsertItem(index, item); ... } protected override void RemoveI 我有一个通用的集合:
public Items : Collection<Object>{   protected overrIDe voID InsertItem(int index,Object item)   {      base.InsertItem(index,item);      ...   }   protected overrIDe voID RemoveItem(int index)   {      base.RemoveItem(index);      ...   }   protected overrIDe voID SetItem(int index,Object item)   {      base.SetItem(index,item);      ...   }   protected overrIDe voID ClearItems()   {      base.ClearItems();      ...   }

现在我需要一种方式来对原来的这个集合进行排序.

奖金喋喋不休

我尝试将我的类转换为使用List< T>而不是集合< T> (因为Collection< T>不支持订单的概念).然后允许调用Sort方法:

this.Items.sort(SortCompareCallback);protected virtual int SortCompareCallback(Object x,Object y){   return OnCompareItems(new SortCompareEventArgs(x,y,this.sortColumnIndex,direction));}

但是当列表被修改时,我会丢失虚拟方法.

我想过使用linq,但问题是:

>我不知道如何从linq表达式调用回调
> linq不排序集合,它只能返回一个新的集合

如何排序一般的Collection< T>?

解决方法 如果您不需要在排序期间调用虚拟覆盖,则应该能够执行以下 *** 作:
class SortableCollection<T> : Collection<T>{    private Readonly List<T> _List;    public SortableCollection() : this(new List<T>()) {}    public SortableCollection(List<T> List) : base(List)    {        _List = List;    }    public voID Sort() { _List.sort(); }}

或这个:

class SortableCollection<T> : Collection<T>{    public SortableCollection() : this(new List<T>()) {}    public SortableCollection(List<T> List) : base(List) {}    public voID Sort() { ((List<T>)Items).sort(); }}
总结

以上是内存溢出为你收集整理的c# – 如何对原位进行排序?全部内容,希望文章能够帮你解决c# – 如何对原位进行排序?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1239523.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存