需要在SortedCollection(C#,2.0)中允许重复

需要在SortedCollection(C#,2.0)中允许重复,c#,collections,duplicates,icollection,C#,Collections,Duplicates,Icollection,我正在处理一个项目,它需要更改“BaseSortedCollection”类以允许重复。该类当前实现IEnumerable、IDisposable、ICollection和ISerializable。“BaseSortedCollection”存储具有ItemID(Int64)的项目,该ID在访问集合时用作密钥。我需要在集合中同时存在两个相同的项(相同的ItemID),并且能够检索到它们 我们正在使用2.0框架 有什么建议吗 提前谢谢 如果需要自动排序,我想您必须扩展一个常规的ArrayList

我正在处理一个项目,它需要更改“BaseSortedCollection”类以允许重复。该类当前实现IEnumerable、IDisposable、ICollection和ISerializable。“BaseSortedCollection”存储具有ItemID(Int64)的项目,该ID在访问集合时用作密钥。我需要在集合中同时存在两个相同的项(相同的ItemID),并且能够检索到它们

我们正在使用2.0框架

有什么建议吗


提前谢谢

如果需要自动排序,我想您必须扩展一个常规的ArrayList,并重写Add方法来调用Sort。然而,我似乎无法理解两件物品具有相同(应该是唯一的)标识号的想法


编辑,或者可能是NameValueCollection(在System.Collections.Specialized中)更合适?扩展它并添加您自己的排序方法…

BaseSortedCollection中的每个项都可以是一个列表(T),因此如果您有两个项具有相同的键,那么您将有一个列表(T),其中包含与该键对应的条目的两个项。

我假设您正在扩展一种不允许双重键的字典

那么这个实现呢。我假设您的项目实现了IComparable

class BaseSortedCollection<T> : Collection<T>, ICollection<T>, IEnumerable<T>,
    System.Collections.ICollection, System.Collections.IEnumerable
    where T : IComparable<T>
{
    /// <summary>
    ///     Adds an item to the Collection<T> at the correct position.
    /// </summary>
    /// <param name="item">The object to add to </param>
    public new void Add(T item)
    {
        int pos = GetInsertPositio(item);
        base.InsertItem(pos, item);
    }


    /// <summary>
    /// Convinience function to add variable number of items in one Functioncall
    /// </summary>
    /// <param name="itemsToBeAdded">The items to be added.</param>
    /// <returns>this to allow fluent interface</returns>
    public AutoSortCollection<T> AddItems(params T[] itemsToBeAdded)
    {
        foreach (var item in itemsToBeAdded)
            Add(item);
        return this;
    }

    /// <summary>
    /// Get position where item should be inserted.
    /// </summary>
    /// <param name="item"></param>
    /// <returns>Get position where item should be inserted.</returns>
    private int GetInsertPositio(T item)
    {
        if (item == null)
            throw new ArgumentNullException();

        for (int pos = this.Count - 1; pos >= 0; pos--)
        {
            if (item.CompareTo(this.Items[pos]) > 0)
                return pos + 1;
        }

        return 0;
    }
}
class BaseSortedCollection:Collection、ICollection、IEnumerable、,
System.Collections.ICollection,System.Collections.IEnumerable
其中T:i可比较
{
/// 
///将项目添加到集合的正确位置。
/// 
///要添加到的对象
新增公共作废(T项)
{
int pos=获取插入位置(项目);
基本插入项(位置、项目);
}
/// 
///方便函数,用于在一个函数调用中添加可变数量的项
/// 
///要添加的项目。
///这将允许流畅的界面
公共AutoSortCollection附加项(参数T[]项已添加)
{
foreach(itemstobedded中的var项)
增加(项目);
归还这个;
}
/// 
///获取项目应插入的位置。
/// 
/// 
///获取项目应插入的位置。
私有int GetInsertPositio(T项)
{
如果(项==null)
抛出新ArgumentNullException();
对于(int pos=this.Count-1;pos>=0;pos--)
{
如果(项目比较)(本项目[pos])>0
返回位置+1;
}
返回0;
}
}
这应该有效(使用MsTest)

//
///sccpackagex构造函数的测试排序
///
[TestMethod()]
公共无效排序测试()
{
BaseSortedCollection集合=新的BaseSortedCollection()。添加项(1,5,3,2,4,0);
aresequal(6,collection.Count,“collection.Count”);

对于(int i=0;i)我发现在不了解BaseSortedCollection的一些实现细节的情况下,很难给出好的建议。为什么重复项不起作用?为什么不使用(可能是平衡的)二叉树作为项目的数据结构?感谢您的回复。重复项不起作用,因为两个项目具有相同的ItemID,这是集合中用作键的项。我更喜欢集合(T)而不是列表(T),因为列表允许myList[20]=会破坏排序的某个项目。请参阅下面的回复
    /// <summary>
    ///A test sorting for SCCPackageEx Constructor
    ///</summary>
    [TestMethod()]
    public void SortingTest()
    {
        BaseSortedCollection<int> collection = new BaseSortedCollection<int>().AddItems(1,5,3,2,4,0);
        Assert.AreEqual(6, collection.Count, "collection.Count");

        for(int i=0; i <=5; i++)
           Assert.AreEqual(i, collection[i], "collection[" + i + "]");
    }