Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何对集合进行排序<;T>;到位了吗?_C#_.net_Sorting - Fatal编程技术网

C# 如何对集合进行排序<;T>;到位了吗?

C# 如何对集合进行排序<;T>;到位了吗?,c#,.net,sorting,C#,.net,Sorting,我有一个通用的收藏: public Items : Collection<Object> { protected override void InsertItem(int index, Object item) { base.InsertItem(index, item); ... } protected override void RemoveItem(int index) { base.RemoveItem(in

我有一个通用的收藏:

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();
      ...
   }
但是当列表被修改时,我会丢失虚拟方法

我曾想过使用Linq,但问题是:

  • 我不知道如何从Linq表达式调用回调
  • Linq不会对集合进行排序,它只能返回一个新集合

如何对通用的
集合进行排序

您可以使用(它还实现了
ICollection
,因此如果您愿意,可以将其视为一个集合)。

如果您想要一个带有内容更改通知的可排序列表,您应该查看
集合的索引器。如果您真的想对项目进行适当的排序,可以使用索引器实现您喜欢的任何排序算法。下面是一个例子,使用适当的集合,可以取O(N^3)

void SortInPlace(集合列)
{

对于(int i=0;i,如果您不需要在排序期间调用虚拟覆盖,您应该能够执行以下操作:

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(); }
}
类可排序集合:集合
{
私有只读列表_List;
公共SortableCollection():此(新列表()){}
公共可排序集合(列表):基本(列表)
{
_列表=列表;
}
public void Sort(){u 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(); }
}
类可排序集合:集合
{
公共SortableCollection():此(新列表()){}
公共可排序集合(列表):基(列表){}
public void Sort(){((列表)项).Sort();}
}

是您可以对集合进行排序尝试以下操作:

public ICollection<T> getSortedData(ICollection<T> collection, string property, string direction)
{
    switch (direction.Trim())
    {
        case "asc":
            collection = ((from n in collection
                           orderby
                           n.GetType().GetProperty(property).GetValue(n, null)
                           select n).ToList<T>()) as ICollection<T>;
        break;
        case "desc":
            collection = ((from n in collection
                           orderby
                           n.GetType().GetProperty(property).GetValue(n, null)
                           descending
                           select n).ToList<T>()) as ICollection<T>;
        break;
    }
    return collection;
}
public ICollection getSortedData(ICollection集合、字符串属性、字符串方向)
{
开关(方向微调())
{
案例“asc”:
集合=((从集合中的n开始)
排序子句
n、 GetType().GetProperty(property).GetValue(n,null)
选择n).ToList())作为ICollection;
打破
案例“desc”:
集合=((从集合中的n开始)
排序子句
n、 GetType().GetProperty(property).GetValue(n,null)
下降的
选择n).ToList())作为ICollection;
打破
}
回收;
}

使用
ArrayList.Adapter(您的集合)
并将其作为数组进行排序。

加载列表后,您可以对其调用排序方法。它有效。您在说什么?有144个答案等待您接受它们。集合的实际实现是什么?它是数组、列表、字典、集合还是某种复杂的嵌套结构?什么?您需要对其进行排序收集数据以公开排序方法,您需要向我们展示这是什么,然后我们才能对如何进行评论。@Servy集合的实际实现是
System.Collections.ObjectModel.collection
@gdoron我应该接受不回答问题的答案,还是继续等待有效答案?这是answer是最好的,但是我注意到
集合
已经有一个
受保护的IList项{get;}
属性。可以消除私有的
\u列表
,将returne
IList
强制转换为
列表
,并对其调用sort。sort方法是
列表
类的成员,因此您必须强制转换受保护属性的返回值;我将编辑代码以反映这种可能性。的文档“Items”表示“在集合周围获得一个IList包装器。"但这不是真的;它实际上公开了集合包装的IList。但是通过反射器,我可以看到,
集合
实际上是在创建一个
列表
。现在,这是一个私有实现细节,随时可能更改。但这就是为什么可以使用您的代码-您知道
返回一个
列表
,因为你一开始就把它交给了这个集合。好吧……我更高兴了。看了这篇文章后:特别是比较代表,以及这个将集合
转换到
列表
的答案,我能够进行排序。对我来说,代码行是这样的:
((列表)项).Sort((左,右)=>string.Compare(左.文本,右.文本,StringComparison.CurrentCulture));
SortedSet是一种方法,因为SortedList基本上是一个字典。而且T项必须实现IComparable。但是如果这样做有效,它是最理想的方法。@Dmitry:一个集合只能有一个给定值的元素(一般来说,不确定.NET实现)。因此,如果将整数1、2、2、3放入一个集合中,集合将包含{1、2、3},但如果将它们放入一个列表中,列表将包含{1、2、2、3}。语义(在集合论中……同样不确定.NET是如何实现它的)集合和列表不同。我只是说,SortedList在.NET中不存在,除非您自己实现。SortedList是SortedDictionary的一个内存效率更高的版本。SortedSet是最接近的可用集合,但正如您所提到的,它不允许重复。谢谢,我非常需要它。我有即时消息使用IEnumerable在扩展符号中对其进行了补全,也用于ThanBy循环中的多个string[]属性。
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(); }
}
public ICollection<T> getSortedData(ICollection<T> collection, string property, string direction)
{
    switch (direction.Trim())
    {
        case "asc":
            collection = ((from n in collection
                           orderby
                           n.GetType().GetProperty(property).GetValue(n, null)
                           select n).ToList<T>()) as ICollection<T>;
        break;
        case "desc":
            collection = ((from n in collection
                           orderby
                           n.GetType().GetProperty(property).GetValue(n, null)
                           descending
                           select n).ToList<T>()) as ICollection<T>;
        break;
    }
    return collection;
}