A causa di questo risponde elevata immissione di ricerca ho pensato che il soluzione LINQ OrderBy è la pena che mostra:
class Person
{
public Person(string firstname, string lastname)
{
FirstName = firstname;
LastName = lastname;
}
public string FirstName { get; set; }
public string LastName { get; set; }
}
static void Main(string[] args)
{
Dictionary<Person, int> People = new Dictionary<Person, int>();
People.Add(new Person("John", "Doe"), 1);
People.Add(new Person("Mary", "Poe"), 2);
People.Add(new Person("Richard", "Roe"), 3);
People.Add(new Person("Anne", "Roe"), 4);
People.Add(new Person("Mark", "Moe"), 5);
People.Add(new Person("Larry", "Loe"), 6);
People.Add(new Person("Jane", "Doe"), 7);
foreach (KeyValuePair<Person, int> person in People.OrderBy(i => i.Key.LastName))
{
Debug.WriteLine(person.Key.LastName + ", " + person.Key.FirstName + " - Id: " + person.Value.ToString());
}
}
uscita:
Doe, John - Id: 1
Doe, Jane - Id: 7
Loe, Larry - Id: 6
Moe, Mark - Id: 5
Poe, Mary - Id: 2
Roe, Richard - Id: 3
Roe, Anne - Id: 4
In questo esempio sarebbe senso utilizzare anche ThenBy per Nomi:
foreach (KeyValuePair<Person, int> person in People.OrderBy(i => i.Key.LastName).ThenBy(i => i.Key.FirstName))
L'uscita è:
Doe, Jane - Id: 7
Doe, John - Id: 1
Loe, Larry - Id: 6
Moe, Mark - Id: 5
Poe, Mary - Id: 2
Roe, Anne - Id: 4
Roe, Richard - Id: 3
LINQ ha anche la OrderByDescending e ThenByDescending per coloro che ne hanno bisogno.
Non sono sicuro di aver capito la tua domanda, in quanto un dizionario non è elencato in alcun ordine? Puoi scorrere le chiavi, o i valori, che puoi facilmente ordinare al volo ... –