C# SortedList

The SortedList is a collection class in C# to store key/ value pairs. The key must be unique and cannot be null. It is sorted by keys.

C# has both generic and non-generic SortedList. Since the generic SortedList<TKey, TValue> is faster, it should be preferred. The generic SortedList class is defined in the System.Collections.Generics namespace:

Create a SortedList

To create a SortedList with an int key and string value pair, use <int, string>. Set the type in angular braces in the case of the generic SortedList.

Let us create a SortedList. The result will get sorted by keys automatically:

using System;
using System.Collections;
using System.Collections.Generic;

namespace Studyopedia
{
  class Program
  {
    static void Main(string[] args)
    {
       SortedList<int, string> sList = new SortedList<int, string>();
       sList.Add(4,"South Africa");
       sList.Add(1,"India");
       sList.Add(5,"England");
       sList.Add(2,"Australia");
       sList.Add(3,"New Zealand");

       foreach (KeyValuePair<int, string> i in sList)
       {
           Console.WriteLine("Key: {0}, Value: {1}", i.Key, i.Value);
       }
     }
  }
}

Output

Key: 1, Value: India
Key: 2, Value: Australia
Key: 3, Value: New Zealand
Key: 4, Value: South Africa
Key: 5, Value: England

Access SortedList elements

To access a value from a SortedList, use the key value. Set the key of the value you want to access as an indexer. Let us see an example:

using System;
using System.Collections;
using System.Collections.Generic;

namespace Studyopedia
{
  class Program
  {
    static void Main(string[] args)
    {
       SortedList<int, string> sList = new SortedList<int, string>();
       sList.Add(4,"South Africa");
       sList.Add(1,"India");
       sList.Add(5,"England");
       sList.Add(2,"Australia");
       sList.Add(3,"New Zealand");

      foreach (KeyValuePair<int, string> i in sList)
       {
           Console.WriteLine("Key: {0}, Value: {1}", i.Key, i.Value);
       }
       
       Console.WriteLine("Value with key 2 = "+sList[2]);
     }
  }
}

Output

Key: 1, Value: India
Key: 2, Value: Australia
Key: 3, Value: New Zealand
Key: 4, Value: South Africa
Key: 5, Value: England
Value with key 2 = Australia

Delete a SortedList element

To delete a specific key/ value pair, set the key as a parameter of the Remove() method. Let us see an example and delete a key/ value par:

using System;
using System.Collections;
using System.Collections.Generic;

namespace Studyopedia
{
  class Program
  {
    static void Main(string[] args)
    {
       SortedList<int, string> sList = new SortedList<int, string>();
       sList.Add(4,"South Africa");
       sList.Add(1,"India");
       sList.Add(5,"England");
       sList.Add(2,"Australia");
       sList.Add(3,"New Zealand");

       Console.WriteLine("\nSortedList...\n");
       foreach (KeyValuePair<int, string> i in sList)
       {
           Console.WriteLine("Key: {0}, Value: {1}", i.Key, i.Value);
       }
       
       Console.WriteLine("\nRemoving key value pair with key 3");
       sList.Remove(3);
       
       Console.WriteLine("\nUpdated SortedList...\n");
       foreach (KeyValuePair<int, string> i in sList)
       {
           Console.WriteLine("Key: {0}, Value: {1}", i.Key, i.Value);
       }    
     }
  }
}

Output

SortedList...

Key: 1, Value: India
Key: 2, Value: Australia
Key: 3, Value: New Zealand
Key: 4, Value: South Africa
Key: 5, Value: England

Removing key value pair with key 3

Updated SortedList...

Key: 1, Value: India
Key: 2, Value: Australia
Key: 4, Value: South Africa
Key: 5, Value: England

Display only the keys

To display only the keys from the key/ value pair of a SortedList, use the keys property. Let us see an example:

using System;
using System.Collections;
using System.Collections.Generic;

namespace Studyopedia
{
  class Program
  {
    static void Main(string[] args)
    {
       SortedList<int, string> sList = new SortedList<int, string>();
       sList.Add(4,"South Africa");
       sList.Add(1,"India");
       sList.Add(5,"England");
       sList.Add(2,"Australia");
       sList.Add(3,"New Zealand");

       Console.WriteLine("\nSortedList...");
       foreach (KeyValuePair<int, string> i in sList)
       {
           Console.WriteLine("Key: {0}, Value: {1}", i.Key, i.Value);
       }
       
       Console.WriteLine("\nSortedList Keys...");
        
       for (int i = 0; i < sList.Count; i++)
        {
           Console.WriteLine(sList.Keys[i]);
        }
     }
  }
}

Output

SortedList...
Key: 1, Value: India
Key: 2, Value: Australia
Key: 3, Value: New Zealand
Key: 4, Value: South Africa
Key: 5, Value: England

SortedList Keys...
1
2
3
4
5

Display only the values

To display only the values from the key/ value pair of a SortedList, use the Values property. Let us see an example:

using System;
using System.Collections;
using System.Collections.Generic;

namespace Studyopedia
{
  class Program
  {
    static void Main(string[] args)
    {
       SortedList<int, string> sList = new SortedList<int, string>();
       sList.Add(4,"South Africa");
       sList.Add(1,"India");
       sList.Add(5,"England");
       sList.Add(2,"Australia");
       sList.Add(3,"New Zealand");

       Console.WriteLine("\nSortedList...");
       foreach (KeyValuePair<int, string> i in sList)
       {
           Console.WriteLine("Key: {0}, Value: {1}", i.Key, i.Value);
       }
       
       Console.WriteLine("\nSortedList Values...");
        
       for (int i = 0; i < sList.Count; i++)
        {
           Console.WriteLine(sList.Values[i]);
        }
     }
  }
}

Output

SortedList...
Key: 1, Value: India
Key: 2, Value: Australia
Key: 3, Value: New Zealand
Key: 4, Value: South Africa
Key: 5, Value: England

SortedList Values...
India
Australia
New Zealand
South Africa
England

Check the SortedList for a specific key

The ContainsKey() method allows you to check a SortedList for a key. Let us see an example:

using System;
using System.Collections;
using System.Collections.Generic;

namespace Studyopedia
{
  class Program
  {
    static void Main(string[] args)
    {
       SortedList<int, string> sList = new SortedList<int, string>();
       sList.Add(4,"South Africa");
       sList.Add(1,"India");
       sList.Add(5,"England");
       sList.Add(2,"Australia");
       sList.Add(3,"New Zealand");

       Console.WriteLine("\nSortedList...");
       foreach (KeyValuePair<int, string> i in sList)
       {
           Console.WriteLine("Key: {0}, Value: {1}", i.Key, i.Value);
       }
       
       Console.WriteLine("\nKey 5 is present? "+sList.ContainsKey(5));
     }
  }
}

Output

SortedList...
Key: 1, Value: India
Key: 2, Value: Australia
Key: 3, Value: New Zealand
Key: 4, Value: South Africa
Key: 5, Value: England

Key 5 is present? True

Check the SortedList for a specific value

The ContainsValue() method allows you to check a SortedList for a value. Let us see an example:

using System;
using System.Collections;
using System.Collections.Generic;

namespace Studyopedia
{
  class Program
  {
    static void Main(string[] args)
    {
       SortedList<int, string> sList = new SortedList<int, string>();
       sList.Add(4,"South Africa");
       sList.Add(1,"India");
       sList.Add(5,"England");
       sList.Add(2,"Australia");
       sList.Add(3,"New Zealand");

       Console.WriteLine("\nSortedList...");
       foreach (KeyValuePair<int, string> i in sList)
       {
           Console.WriteLine("Key: {0}, Value: {1}", i.Key, i.Value);
       }
       
       Console.WriteLine("\nValue Australia is present? "+sList.ContainsValue("Australia"));
     }
  }
}

Output

SortedList...
Key: 1, Value: India
Key: 2, Value: Australia
Key: 3, Value: New Zealand
Key: 4, Value: South Africa
Key: 5, Value: England

Value Australia is present? True

Read More

C# ArrayList
C# Dictionary
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment