1
SortedList<string, int> list = new SortedList<string, int>;
list.Add("abc", 2);
list.Add("def", 3);

string index = list.GetIndex(2) ???????
Console.WriteLine(index);

I'm really confused. How can I get an array's index by using the index's value??

Output should be

abc

Solutions:

Use the IndexOfValue method that SortedList exposed, with combination of the Linq extension method ElementAt(int index), so:

string index = list.ElementAt(list.IndexOfValue(value)).Key;//output will be abc

---o---

This also works

string index = list.Keys.ElementAt<string>(list.IndexOfValue(2));
3
  • 4
    I'm just as confused as you are. What array? I don't see any array. Commented Aug 24, 2011 at 13:29
  • 2
    If the 2 and 3 are just index references, why not just use a list of strings, or a dictionary of int, string? Commented Aug 24, 2011 at 13:32
  • Actually I've got an SortedList with ToolStripMenuItem as index and Form as value. Commented Aug 24, 2011 at 13:35

5 Answers 5

1

Use the IndexOfValue method that SortedList exposed, with combination of the Linq extension method ElementAt(int index), so:

string index = list.ElementAt(list.IndexOfValue(value)).Key;//output will be abc
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you ! This also works (I've just found it) string index = list.Keys.ElementAt<string>(list.IndexOfValue(2));
1

MSDN Documentation about SortedList.GetByIndex method:

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.3, "fox" );
      mySL.Add( 1.4, "jumped" );
      mySL.Add( 1.5, "over" );
      mySL.Add( 1.2, "brown" );
      mySL.Add( 1.1, "quick" );
      mySL.Add( 1.0, "The" );
      mySL.Add( 1.6, "the" );
      mySL.Add( 1.8, "dog" );
      mySL.Add( 1.7, "lazy" );

      // Gets the key and the value based on the index.
      int myIndex=3;
      Console.WriteLine( "The key   at index {0} is {1}.", myIndex, mySL.GetKey( myIndex ) );
      Console.WriteLine( "The value at index {0} is {1}.", myIndex, mySL.GetByIndex( myIndex ) );

      // Gets the list of keys and the list of values.
      IList myKeyList = mySL.GetKeyList();
      IList myValueList = mySL.GetValueList();

      // Prints the keys in the first column and the values in the second column.
      Console.WriteLine( "\t-KEY-\t-VALUE-" );
      for ( int i = 0; i < mySL.Count; i++ )
         Console.WriteLine( "\t{0}\t{1}", myKeyList[i], myValueList[i] );
   }
}
/* 
This code produces the following output.

The key   at index 3 is 1.3.
The value at index 3 is fox.
    -KEY-    -VALUE-
    1    The
    1.1    quick
    1.2    brown
    1.3    fox
    1.4    jumped
    1.5    over
    1.6    the
    1.7    lazy
    1.8    dog
*/ 

3 Comments

I think this is not supported in NET 4.0
Refer this link. It clearly states that it is supported in all versions of .Net.
1

Based on your statement that the output should be abc, I think you really want the text, not the index. To get the text of an item based on its value, simply do the following:

SortedList<string, int> list = new SortedList<string, int>();
list.Add("abc", 2);
list.Add("def", 3);
string text = list.ElementAt(listz.IndexOfValue(2)).Key
Console.WriteLine(text); 

3 Comments

There's no Property like Item in SortedList -_-
invalid arguments. list is based on string not integer.
Sorry, you are absolutely right. It's correct now! I tested it and it returns abc.
0

Use the SortedList.Values property to get an ICollection object containing the values in the SortedList object.

Comments

0

Give this a shot:

SortedList<string, int> list = new SortedList<string, object>();
list.Add("One", 1);
list.Add("Two", 2);                               

int value = (int)list.ElementAt(1).Value;

Or...

int value = list.IndexOfKey("One");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.