SortedList
The SortedList class represents a collection of key-and-value pairs that are sorted by the keys and are accessible by key and by index.
A sorted list is a combination of an array and a hash table. It contains a list of items that can be accessed using a key or an index. If you access items using an index, it is an ArrayList, and if you access items using a key, it is a Hashtable. The collection of items is always sorted by the key value.
Several Commonly Used Methods Defi ned by SortedList :
Method | Description |
---|---|
public virtual bool ContainsKey(object key) | Returns true if key is a key in the invoking SortedList. Returns false otherwise. |
public virtual bool ContainsValue(object value) |
Returns true if value is a value in the invoking SortedList. Returns false otherwise. |
public virtual object GetByIndex(int index) | Returns the value at the index specified by index. |
public virtual IDictionaryEnumerator GetEnumerator( ) |
Returns an IDictionaryEnumerator for the invoking SortedList. |
public virtual object GetKey(int index) | Returns the value of the key at the index specified by index. |
public virtual IList GetKeyList( ) | Returns an IList collection of the keys in the invoking SortedList. |
public virtual IList GetValueList( ) | Returns an IList collection of the values in the invoking SortedList. |
public virtual int IndexOfKey(object key) | Returns the index of the key specified by key. Returns –1 if the key is not in the list. |
public virtual int IndexOfValue(object value) | Returns the index of the first occurrence of the value specified by value. Returns –1 if the value is not in the list. |
public virtual void SetByIndex(int index, object value) |
Sets the value at the index specified by index to the value passed in value. |
public static SortedList Synchronized(SortedList list) |
Returns a synchronized version of the SortedList passed in list. |
public virtual void TrimToSize( ) | Sets Capacity to Count. |
Example
using System; using System.IO; using System.Collections; namespace ConsoleApplication1 { class Program { static void Main() { // Create a sorted SortedList. SortedList sl = new SortedList(); // Add elements to the table sl.Add("house", "Dwelling"); sl.Add("car", "Means of transport"); sl.Add("book", "Collection of printed words"); sl.Add("apple", "Edible fruit"); // Can also add by using the indexer. sl["tractor"] = "Farm implement"; // Get a collection of the keys. ICollection c = sl.Keys; // Use the keys to obtain the values. Console.WriteLine("Contents of list via indexer."); foreach (string str in c) Console.WriteLine(str + ": " + sl[str]); Console.WriteLine(); // Display list using integer indexes. Console.WriteLine("Contents by integer indexes."); for (int i = 0; i < sl.Count; i++) Console.WriteLine(sl.GetByIndex(i)); Console.WriteLine(); // Show integer indexes of entries. Console.WriteLine("Integer indexes of entries."); foreach (string str in c) Console.WriteLine(str + ": " + sl.IndexOfKey(str)); Console.Read(); } } }