Skip to content

37. System.Collections.SortedList

February 13, 2011

The class SortedList implements a sorted list based on a key without possibility of duplicates, all keys must be unique. Each element is a key/value pair accessible as a DictionaryEntry object, either by key or by value.

The sequence of the index is based on the sequence of the sort order. When an element is added, it is inserted in the SortedList in the adequate sort order, and the index is consequently adjusted. When an element is removed, the index is also adjusted. Consequently, the index of a specific key/value pair can change if the there are inserts or removal of elements in the SortedList

The class SortedList implements the interfaces IDictionary, ICollection and IEnumerable.

Declaration/Instanciation/Insertion of items in SortedList then techniques for iterating

The two classes corresponding to System.Collections.SortedList: System.Collections.Generic.SortedList and System.Collections.Generic.SortedDictionary have similar object models. The domain where the two classes have differences is on the use of memory and the speed of insertions and removals:

  • SortedList uses less memory than SortedDictionary.
  • SortedDictionary inserts and removes elements faster on non sorted data.
  • If the list is filled in one go based on sorted data, SortedList is faster than SortedDictionary.

Happy Programing! =)

From → OOP

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: