Export (0) Print
Expand All

SortedList.IndexOfKey Method

Returns the zero-based index of the specified key in the SortedList.

[Visual Basic]
Public Overridable Function IndexOfKey( _
   ByVal key As Object _
) As Integer
[C#]
public virtual int IndexOfKey(
 object key
);
[C++]
public: virtual int IndexOfKey(
 Object* key
);
[JScript]
public function IndexOfKey(
   key : Object
) : int;

Parameters

key
The key to locate in the SortedList.

Return Value

The zero-based index of key, if key is found in the SortedList; otherwise, -1.

Exceptions

Exception Type Condition
ArgumentNullException key is a null reference (Nothing in Visual Basic).
InvalidOperationException The comparer throws an exception.

Remarks

The elements of a SortedList are sorted by the keys either according to a specific IComparer implementation specified when the SortedList is created or according to the IComparable implementation provided by the keys themselves.

The index sequence is based on the sort sequence. When an element is added, it is inserted into SortedList in the correct sort order, and the indexing adjusts accordingly. When an element removed, the indexing also adjusts accordingly. Therefore, the index of a specific key-and-value pair might change as elements are added or removed from the SortedList.

This method uses a binary search algorithm; therefore, the average execution time is proportional to Log2 (n), where n is Count.

Example

[Visual Basic, C#, C++] The following example shows how to determine the index of a key or a value in a SortedList.

[Visual Basic] 
Imports System
Imports System.Collections
Imports Microsoft.VisualBasic

Public Class SamplesSortedList    
    
    Public Shared Sub Main()
        
        ' Creates and initializes a new SortedList.
        Dim mySL As New SortedList()
        mySL.Add(1, "one")
        mySL.Add(3, "three")
        mySL.Add(2, "two")
        mySL.Add(4, "four")
        mySL.Add(0, "zero")
        
        ' Displays the values of the SortedList.
        Console.WriteLine("The SortedList contains the " & _
           "following values:")
        PrintIndexAndKeysAndValues(mySL)
        
        ' Searches for a specific key.
        Dim myKey As Integer = 2
        Console.WriteLine("The key ""{0}"" is at index {1}.", myKey, _
           mySL.IndexOfKey(myKey))
        
        ' Searches for a specific value.
        Dim myValue As String = "three"
        Console.WriteLine("The value ""{0}"" is at index {1}.", myValue, _
           mySL.IndexOfValue(myValue))
    End Sub    
    
    
    Public Shared Sub PrintIndexAndKeysAndValues(myList As SortedList)
        Console.WriteLine(ControlChars.Tab & "-INDEX-" & ControlChars.Tab & _
           "-KEY-" & ControlChars.Tab & "-VALUE-")
        Dim i As Integer
        For i = 0 To myList.Count - 1
            Console.WriteLine(ControlChars.Tab & "[{0}]:" & ControlChars.Tab & _
               "{1}" & ControlChars.Tab & "{2}", i, myList.GetKey(i), _
               myList.GetByIndex(i))
        Next i
        Console.WriteLine()
    End Sub
End Class

' This code produces the following output.
' 
' The SortedList contains the following values:
'     -INDEX-    -KEY-    -VALUE-
'     [0]:    0    zero
'     [1]:    1    one
'     [2]:    2    two
'     [3]:    3    three
'     [4]:    4    four
'
' The key "2" is at index 2.
' The value "three" is at index 3.

[C#] 
using System;
using System.Collections;
public class SamplesSortedList  {

   public static void Main()  {

      // Creates and initializes a new SortedList.
      SortedList mySL = new SortedList();
      mySL.Add( 1, "one" );
      mySL.Add( 3, "three" );
      mySL.Add( 2, "two" );
      mySL.Add( 4, "four" );
      mySL.Add( 0, "zero" );

      // Displays the values of the SortedList.
      Console.WriteLine( "The SortedList contains the following values:" );
      PrintIndexAndKeysAndValues( mySL );

      // Searches for a specific key.
      int myKey = 2;
      Console.WriteLine( "The key \"{0}\" is at index {1}.", myKey, mySL.IndexOfKey( myKey ) );

      // Searches for a specific value.
      String myValue = "three";
      Console.WriteLine( "The value \"{0}\" is at index {1}.", myValue, mySL.IndexOfValue( myValue ) );
   }


   public static 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();
   }
}
/* 
This code produces the following output.

The SortedList contains the following values:
    -INDEX-    -KEY-    -VALUE-
    [0]:    0    zero
    [1]:    1    one
    [2]:    2    two
    [3]:    3    three
    [4]:    4    four

The key "2" is at index 2.
The value "three" is at index 3.
*/ 

[C++] 
#using <mscorlib.dll>
#using <system.dll>

using namespace System;
using namespace System::Collections;

void PrintIndexAndKeysAndValues( SortedList* myList )  {
   Console::WriteLine( S"\t-INDEX-\t-KEY-\t-VALUE-" );
   for ( int i = 0; i < myList->Count; i++ )  {
      Console::WriteLine( S"\t[{0}]:\t{1}\t{2}",
         __box(i), myList->GetKey(i), myList->GetByIndex(i) );
   }
   Console::WriteLine();
}

int main()  {

   // Creates and initializes a new SortedList.
   SortedList* mySL = new SortedList();
   mySL->Add( __box(1), S"one" );
   mySL->Add( __box(3), S"three" );
   mySL->Add( __box(2), S"two" );
   mySL->Add( __box(4), S"four" );
   mySL->Add( __box(0), S"zero" );

   // Displays the values of the SortedList.
   Console::WriteLine( S"The SortedList contains the following values:" );
   PrintIndexAndKeysAndValues( mySL );

   // Searches for a specific key.
   int myKey = 2;
   Console::WriteLine( S"The key \"{0}\" is at index {1}.",
      __box(myKey), __box(mySL->IndexOfKey( __box(myKey) )) );

   // Searches for a specific value.
   String* myValue = S"three";
   Console::WriteLine( S"The value \"{0}\" is at index {1}.",
      myValue, __box(mySL->IndexOfValue( myValue )) );
}

/*
This code produces the following output.

The SortedList contains the following values:
        -INDEX- -KEY-   -VALUE-
        [0]:    0       zero
        [1]:    1       one
        [2]:    2       two
        [3]:    3       three
        [4]:    4       four

The key "2" is at index 2.
The value "three" is at index 3.
*/

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

SortedList Class | SortedList Members | System.Collections Namespace | ContainsKey | IndexOfValue | Performing Culture-Insensitive String Operations in Collections

Show:
© 2014 Microsoft