7

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!

  • 1
    I find it difficult to give good advice without knowing some implementation details of your BaseSortedCollection. Why are duplicates not working in the first place? Why not use a (maybe balanced) binary tree as data structure for your items? – Lucero Apr 08 '09 at 15:33
  • Thanks for your response. The duplicates arent working because two items have the same ItemID, which is what is being used as the key in the collection. –  Apr 08 '09 at 15:43

3 Answers3

5

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.

Meta-Knight
  • 17,626
  • 1
  • 48
  • 58
  • i prefer to have a Collection(T) instead of a List(T) because a list would allow myList[20] = someItem that would destroy the sorting. see my reply below – k3b Dec 09 '10 at 12:34
0

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 + "]");
    }
k3b
  • 14,517
  • 7
  • 53
  • 85
-1

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...

Björn
  • 29,019
  • 9
  • 65
  • 81