how do you sort dictionary in c#?
the key will be string, the value will be listviewitem. i want to sort by the key.

i cant use sorted dictionary. give a complete working example please, preferably attach full project for me :)

Thanks in advance.

i couldnt sort it using dictionary but i was able to sort using a list. here is the example :

class ListItem : IComparable
		{
			private string columnValue = string.Empty;
			private ListViewItem item = null;
			public ListViewItem Item
			{
				get
				{
					return item;
				}
			}
			public ListItem(string _columnValue,ListViewItem _item)
			{
				columnValue = _columnValue;
				item = _item;
			}

			#region IComparable Members

			public int CompareTo(object obj)
			{
				if (obj is ListItem)
				{
					ListItem li = (ListItem)obj;
					return columnValue.CompareTo(li.columnValue);
				}
				throw new ArgumentException("object is not ListItem");
			}

			#endregion
		}

		private bool sortDirectionUp = false;

		private void smartListView1_ColumnClick(object sender, ColumnClickEventArgs e)
		{

			List<ListItem> listitems = new List<ListItem>();

			foreach (ListViewItem lvi in smartListView1.Items)
			{
				listitems.Add(new ListItem(lvi.SubItems[e.Column].Text, lvi));
			}

			if (!sortDirectionUp)
			{
				listitems.Sort();
				sortDirectionUp = true;
			}
			else
			{
				listitems.Reverse();
				sortDirectionUp = false;
			}

			smartListView1.Items.Clear();
			foreach (ListItem li in listitems)
			{
				smartListView1.Items.Add(li.Item);
			}
		}

seeing it work made me pretty happy :)

Glad you're happy Serkan:)
If you want your dictionary always sorted you could use SortedDictionary like this:

class Program
    {
        static void Main(string[] args)
        {
            
            // Make a new dictionary of strings, with int keys.
            Dictionary<int, string> MyCoctails = new Dictionary<int, string>();

            // Get the keys with the Keys property.
            Dictionary<int, string>.KeyCollection keyColl = MyCoctails.Keys;

            MyCoctails.Add(3, "mojito");
            MyCoctails.Add(2, "malibu sunrise");
            MyCoctails.Add(5, "white lady");
            MyCoctails.Add(1, "margarita");

         
            // The elements of the KeyCollection are strongly typed
            // with the type that was specified for dictionary keys.
            Console.WriteLine("UNSORTED DICTIONARY:");
            foreach( int i in keyColl )
            {
                Console.WriteLine("Key = {0}", i);
            }

            // When you use foreach to enumerate dictionary elements,
            // the elements are retrieved as KeyValuePair objects.
            Console.WriteLine();
            foreach (KeyValuePair<int, string> kvp in MyCoctails)
            {
                Console.WriteLine("Key = {0}, Value = {1}", 
                    kvp.Key, kvp.Value);
            }
            ///////////////////////////////////////////////////////////
            
            // Make a new sorted dictionary of strings, with int keys.
            SortedDictionary<int, string> MySortedCoctails = new SortedDictionary<int, string>();

            // Get the keys with the Keys property.
            SortedDictionary<int, string>.KeyCollection SortedkeyColl = MySortedCoctails.Keys;
            MySortedCoctails.Add(3, "mojito");
            MySortedCoctails.Add(2, "malibu sunrise");
            MySortedCoctails.Add(5, "white lady");
            MySortedCoctails.Add(1, "margarita");

            // The elements of the KeyCollection are strongly typed
            // with the type that was specified for dictionary keys.
            Console.WriteLine("SORTED DICTIONARY:");
            foreach (int i in SortedkeyColl)
            {
                Console.WriteLine("Key = {0}", i);
            }

            // When you use foreach to enumerate dictionary elements,
            // the elements are retrieved as KeyValuePair objects.
            Console.WriteLine();
            foreach (KeyValuePair<int, string> kvp in MySortedCoctails)
            {
                Console.WriteLine("Key = {0}, Value = {1}",
                    kvp.Key, kvp.Value);
            }
            Console.ReadKey();
        }
    }
This question has already been answered. Start a new discussion instead.