lima-city: Webhosting, Domains und Cloud
1 Pluspunkt 0 Minuspunkte

Ich habe eine Klasse

public class MyClass
{
    public int ID { get; set; }
    public string Name { get; set; }
    //...
}

und möchte diese in einem Dictionary speichern.

Dictionary<int, MyClass> myDictionary = new Dictionary<int, MyClass>
{
    { 1, new MyClass { ID = 1, Name = "Objekt 1" } },
    { 2, new MyClass { ID = 2, Name = "Objekt 2" } },
    { 3, new MyClass { ID = 3, Name = "Objekt 3" } }
};

Wie kann ich das Dictionary anhand der Werte in MyClass->ID sortieren?

von  

4 Antworten

0 Pluspunkte 0 Minuspunkte

Dazu kannst du Linq verwenden.

var sortedDictionary = myDictionary.OrderBy(kv => kv.Value.ID).ToDictionary(kv => kv.Key, kv => kv.Value);

von (406 Punkte)  
0 Pluspunkte 0 Minuspunkte

Du kannst das Dictionary in eine List<KeyValuePair> umwandeln.

List<KeyValuePair<int, MyClass>> keyValueList = new List<KeyValuePair<int, MyClass>>(myDictionary);

Die kannst du dann anhand der ID sortieren.

for (int i = 0; i < keyValueList.Count - 1; i++)
{
    for (int j = 0; j < keyValueList.Count - i - 1; j++)
    {
        if (keyValueList[j].Value.ID > keyValueList[j + 1].Value.ID)
        {
            // Tausche Elemente
            var temp = keyValueList[j];
            keyValueList[j] = keyValueList[j + 1];
            keyValueList[j + 1] = temp;
        }
    }
}

und wieder zu einem Dictionary zurück umwandeln.

Dictionary<int, MyClass> sortedDictionary = new Dictionary<int, MyClass>();
foreach (var pair in keyValueList)
{
    sortedDictionary.Add(pair.Key, pair.Value);
}
von (547 Punkte)  
0 Pluspunkte 0 Minuspunkte
I've been exploring terpene botanicals - https://terpenewarehouse.com/collections/terpene-enhancer-liquefier  recently, and I'm really enjoying the experience. The scents are with, real, and pleasant. They add a nice touch to my daily drill, ration congeal the atmosphere and atmosphere. A great find to save anyone who appreciates perfumed wellness tools.
von  
0 Pluspunkte 0 Minuspunkte
The flavor options against these CBD gummies and https://www.nothingbuthemp.net/pages/do-thca-hash-holes-get-you-high  are impressive. Each chequer tastes health degree than overly phony, and the feel is pleasantly chewy. The jar seals tensely, which keeps them alternative and easy as pie to stockpile looking for longer periods.
von  
Diese Community basiert auf dem Prinzip der Selbstregulierung. Beiträge werden von Nutzern erstellt, bewertet und verbessert – ganz ohne zentrale Moderation.

Wer hilfreiche Fragen stellt oder gute Antworten gibt, sammelt Punkte. Mit steigender Punktzahl erhalten Mitglieder automatisch mehr Rechte, zum Beispiel

  • Kommentare verfassen
  • Fragen und Antworten bewerten
  • Themen von Fragen bearbeiten
  • Fragen, Antworten und Kommentare bearbeiten
  • Inhalte ausblenden

So entsteht eine Plattform, auf der sich Qualität durchsetzt – getragen von einer engagierten Gemeinschaft.

2,582 Fragen

3,103 Antworten

293 Kommentare

120 Nutzer