2017-10-14 6 views
2

カテゴリの異なるアイテムのコレクションがあります。 例 -カテゴリを同時に並べ替え

 dogs - Shepherd, Sheepdog, Lapphund 
    birds - owl, parrot goose 
    cats - misty , tigger, kitty 

私はマップにこれらを保存し、カテゴリと名の両方の昇順で一覧を表示する必要があります。 期待出力 -

birds - 
    owl 
    goose 
    parrot 
    cats - 
    kitty 
    misty 
    tigger 
    dogs - 
    Lapphund 
    Sheepdog 
    Shepherd 

私のコード -

class Animal : IComparable{ 
    private string name; 
    public Animal(string name){ 
     this.name = name; 
    } 
    public string Name{ 
     get{ 
      return this.name; 
     } 
     set{ 
      this.name = value; 
     } 
    } 
    public int CompareTo(object obj) { 
    if (obj == null) return 1; 

    Animal otherAnimal = obj as Animal; 
    if (otherAnimal != null) 
     return this.name.CompareTo(otherAnimal.name); 
    else 
     return 1; 
    } 
} 

しかし、私はまた、最初のそれは鳥、そして猫その後、犬を表示する必要があるよう

+0

'Animal'クラスの定義を質問に追加できますか? –

答えて

2

カテゴリをソートするには、カテゴリをソートしたいですソートされた辞書をそのまま使用することができます

作成 -

SortedDictionary<string,Animal> = new SortedDictionary<string,Animal>(); 

Icomparableを使用して、カテゴリのリスト名をソートします。

となります。

+0

ありがとうございます –

関連する問題