Question

I have a project that I'm working on that requires changing a 'BaseSortedCollection' class to allow duplicates. The class currently implements IEnumerable, IDisposable, ICollection, and ISerializable. The 'BaseSortedCollection' stores Items that have an ItemID (Int64), which is used as the key when accessing the collection. I need to have two identical items (same ItemID) exist in the collection at the same time as well as be able to be retrieved.

We are using the 2.0 framework.

Any suggestions?

Thanks in advance!

Was it helpful?

Solution

I guess you will have to extend a regular ArrayList, and override the Add-method to call Sort if you need the auto sorting. However, I can't seem to wrap my head around the idea of two items with the same (what should be unique) identification number?!

Edit, or maybe NameValueCollection (in System.Collections.Specialized) is more appropriate? Extend it and add your own sorting method...

OTHER TIPS

Each item in your BaseSortedCollection could be a List(T), so if you have two items with the same key, you will have a List(T) containing two items for the entry corresponding to that key.

I assume that you were extending a kind of dictionary that do not allow douplicate keys.

What about this implementation. I Assume that your Item implements 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;
    }
}

this should work (using MsTest)

    /// <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 + "]");
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top