0

So, I've got a Sorted Dictionary where my keys are first names & values are last names.

I have a list-box on my form that displays all possible Values from which the user can pick a last name. However, since first-names are the Keys, these values displayed in the list box aren't sorted.

Andrews -> Carter -> Johnson

Carter -> Johnson -> Andrews

I want the last names to be alphabetically arranged as in the first line, rather than how they are now (the second).

Would creating a List that only contains the Values be the best way to do this? I couldn't find any way to internally sort the contents of a ListBox, so I was at a loss for what else I could do.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
user1993843
  • 155
  • 3
  • 3
  • 13
  • You could simply sort the Dictionary before you fill the ListBox. You have to use LINQ to do that: `myDict.OrderBy(x => x.Key);` – jAC Apr 27 '13 at 15:49
  • See this question http://stackoverflow.com/questions/289/how-do-you-sort-a-c-sharp-dictionary-by-value – Steve Apr 27 '13 at 15:49

1 Answers1

0

You could create another list in the correct order, by something like:

// Given that 'Person' for the sake of this example is:

public class Person
{
    public string FirstName;
    public string LastName;
}

// And you have a dictionary sorted by Person.FirstName:

var dict = new SortedDictionary<string, Person>();
// ...initialise dict...

// Make list reference a List<Person> ordered by the person's LastName

var list = (from entry in dict
            orderby entry.Value.LastName
            select entry.Value).ToList();

// Use list to populate the listbox

This has the advantage of leaving the original collection unmodified (if that's important to you).

The overhead of keeping references to the objects in two different collections is not very high, because it's only one reference per object (plus some fixed overhead for the List<> implementation itself).

But remember that the objects will be kept alive as long as either of the collections are alive (unless you explicitly clear the collections).


Compilable sample:

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

namespace Demo
{
    public static class Program
    {
        private static void Main()
        {
            dict = new SortedDictionary<string, Person>();

            addPerson("iain", "banks");
            addPerson("peter", "hamilton");
            addPerson("douglas", "adams");
            addPerson("arthur", "clark");
            addPerson("isaac", "asimov");

            Console.WriteLine("--------------");

            foreach (var person in dict)
                Console.WriteLine(person.Value);

            var list = (from entry in dict
                        orderby entry.Value.LastName
                        select entry.Value).ToList();

            Console.WriteLine("--------------");

            foreach (var person in list)
                Console.WriteLine(person);
        }

        private static void addPerson(string firstname, string lastname)
        {
            dict.Add(firstname, new Person{FirstName = firstname, LastName = lastname});
        }

        private static SortedDictionary<string, Person> dict;
    }

    public class Person
    {
        public string FirstName;
        public string LastName;
        public override string ToString(){return FirstName + " " + LastName;}
    }
}
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276