Need duplicates allowed in SortedCollection (C #, 2.0)

I have a project that I am working on, which requires changing the BaseSortedCollection class to duplicate. The class currently implements IEnumerable, IDisposable, ICollection, and ISerializable. "BaseSortedCollection" stores items that have an ItemID (Int64) that is used as a key when accessing the collection. I need to have two identical elements (the same ItemID) in the collection at the same time, as well as the possibility of receiving them.

We use environment 2.0.

Any suggestions?

Thanks in advance!

+6
collections c # duplicates icollection
source share
3 answers

I assume you will have to extend the regular ArrayList and override the Add-method to call Sort if you need automatic sorting. However, I seem to be unable to get around the idea of ​​two items with the same (which should be unique) identification number ?!

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

-one
source share

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

+5
source share

I assume that you have expanded a kind of dictionary that does not allow keys to be issued.

How about this implementation. Suppose 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 + "]"); } 
0
source share

All Articles