What we have in this page?
Other SortedList Operations
There are many more operations can be done using Sorted List. The following Tables list the members exposed by the SortedList type that include constructors, properties, methods and interface.
| |||||||||||||||||||||||||||||||||||||
| Public Methods | |
| Symbol | |
| Name | Description |
| Add | Adds an element with the specified key and value to the SortedList. |
| Clear | Removes all elements from the SortedList. |
| Clone | Creates a shallow copy of the SortedList. |
| Contains | Determines whether the SortedList contains a specific key. |
| ContainsKey | Determines whether the SortedList contains a specific key. |
| ContainsValue | Determines whether the SortedList contains a specific value. |
| CopyTo | Copies the SortedList elements to a one-dimensional Array instance at the specified index. |
| Equals | Overloaded. Determines whether two Object instances are equal. (Inherited from Object.) |
| GetByIndex | Gets the value at the specified index of the SortedList. |
| GetEnumerator | Returns an IDictionaryEnumerator that iterates through the SortedList. |
| GetHashCode | Serves as a hash function for a particular type. GetHashCode is suitable for use in hashing algorithms and data structures like a hash table. (Inherited from Object.) |
| GetKey | Gets the key at the specified index of the SortedList. |
| GetKeyList | Gets the keys in the SortedList. |
| GetType | Gets the Type of the current instance. (Inherited from Object.) |
| GetValueList | Gets the values in the SortedList. |
| IndexOfKey | Returns the zero-based index of the specified key in the SortedList. |
| IndexOfValue | Returns the zero-based index of the first occurrence of the specified value in the SortedList. |
| ReferenceEquals | Determines whether the specified Object instances are the same instance. (Inherited from Object.) |
| Remove | Removes the element with the specified key from SortedList. |
| RemoveAt | Removes the element at the specified index of SortedList. |
| SetByIndex | Replaces the value at a specific index in the SortedList. |
| Synchronized | Returns a synchronized (thread safe) wrapper for the SortedList. |
| ToString | Returns a String that represents the current Object. (Inherited from Object.) |
| TrimToSize | Sets the capacity to the actual number of elements in the SortedList. |
|
Table 8 | |
| Protected Methods | |
| Symbol | |
| Name | Description |
| Finalize | Allows an Object to attempt to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.) |
| MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) |
|
Table 9 | |
| Explicit Interface Implementations | |
| Symbol | |
| Name | Description |
| System.Collections.IEnumerable.GetEnumerator | Returns an IEnumerator that iterates through the SortedList. |
|
Table 10 | |
Note: You may find that in C++ .Net the dot (.) is replaced by scope operator (::). The following example shows how to add elements to the SortedList.
// SortedList.cpp : main project file.
#include "stdafx.h"
using namespace System;
using namespace System::Collections;
void PrintKeysAndValues(SortedList^ myList)
{
Console::WriteLine("\t-KEY-\t-VALUE-");
for (int i = 0; i < myList->Count; i++)
{
Console::WriteLine("\t{0}:\t{1}", myList->GetKey(i), myList->GetByIndex(i));
}
Console::WriteLine();
}
int main(array<System::String ^> ^args)
{
// Creates and initializes a new SortedList.
SortedList^ mySL = gcnew SortedList;
mySL->Add("one", "The");
mySL->Add("two", "quick");
mySL->Add("three", "and");
mySL->Add("four", "dirty");
mySL->Add("five", "method");
// Displays the SortedList.
Console::WriteLine("The SortedList contains:");
PrintKeysAndValues(mySL);
return 0;
}
Output:

The following example shows how to remove elements from the SortedList.
// SortedList.cpp : main project file.
#include "stdafx.h"
using namespace System;
using namespace System::Collections;
void PrintKeysAndValues(SortedList^ myList)
{
Console::WriteLine("\t-KEY-\t-VALUE-");
for (int i = 0; i < myList->Count; i++)
{
Console::WriteLine("\t{0}:\t{1}", myList->GetKey(i), myList->GetByIndex(i));
}
Console::WriteLine();
}
int main(array<System::String ^> ^args)
{
// Creates and initializes a new SortedList.
SortedList^ mySL = gcnew SortedList;
mySL->Add("3c", "dog");
mySL->Add("2c", "over");
mySL->Add("1c", "brown");
mySL->Add("1a", "The");
mySL->Add("1b", "quick");
mySL->Add("3a", "and");
mySL->Add("3b", "dirty");
mySL->Add("2a", "learning");
mySL->Add("2b", "method");
// Displays the SortedList.
Console::WriteLine("The SortedList initially contains:");
PrintKeysAndValues(mySL);
// Removes the element with the key "3b".
mySL->Remove("3b");
// Displays the current state of the SortedList.
Console::WriteLine("After removing \"dirty\":");
PrintKeysAndValues(mySL);
// Removes the element at index 5.
mySL->RemoveAt(5);
// Displays the current state of the SortedList.
Console::WriteLine("After removing the element at index 5:");
PrintKeysAndValues(mySL);
return 0;
}
Output:
![]() |
The following example shows how to determine whether the SortedList contains a specific element.
// SortedList.cpp : main project file.
#include "stdafx.h"
using namespace System;
using namespace System::Collections;
void PrintIndexAndKeysAndValues(SortedList^ myList)
{
Console::WriteLine("\t-INDEX-\t-KEY-\t-VALUE-");
for (int i = 0; i < myList->Count; i++)
{
Console::WriteLine("\t[{0}]:\t{1}\t{2}", i, myList->GetKey(i), myList->GetByIndex(i));
}
Console::WriteLine();
}
int main(array<System::String ^> ^args)
{
// Creates and initializes a new SortedList.
SortedList^ mySL = gcnew SortedList;
mySL->Add(2, "two");
mySL->Add(4, "four");
mySL->Add(1, "one");
mySL->Add(3, "three");
mySL->Add((int^)0, "zero");
// Displays the values of the SortedList.
Console::WriteLine("The SortedList contains:" );
PrintIndexAndKeysAndValues(mySL);
// Searches for a specific key.
int myKey = 2;
Console::WriteLine("The key \"{0}\" is {1}.", myKey, mySL->ContainsKey(myKey) ? (String^)"in the SortedList":"NOT in the SortedList");
myKey = 6;
Console::WriteLine("The key \"{0}\" is {1}.", myKey, mySL->ContainsKey(myKey) ? (String^)"in the SortedList":"NOT in the SortedList");
// Searches for a specific value.
String^ myValue = "three";
Console::WriteLine("The value \"{0}\" is {1}.", myValue, mySL->ContainsValue(myValue) ? (String^)"in the SortedList":"NOT in the SortedList");
myValue = "nine";
Console::WriteLine("The value \"{0}\" is {1}.", myValue, mySL->ContainsValue(myValue) ? (String^)"in the SortedList":"NOT in the SortedList");
return 0;
}
Output:
