2016-03-25 6 views
0

アルファベットグループと、私はクラスのユーザーレビューソートリスト、

class Guest 
{ 
    bool f = true; 
    bool g = false; 
    bool s = false; 
    string name = ""; 
} 

そして、すべて含まれてい一覧 - >var g = new List<Guest>(); それは唯一のブール値の本当のことができます。

最初にfのゲストが来て、gのゲストは途中で、最後はゲストです。 しかし、すべてのゲストは、fまたはgまたはaグループのアルファベット順にソートする必要があります。

多分そうですか?

var query = (from Guest in GuestList 
          orderby Guest.f, Guest.g, Guest.s, Guest.name 
          select Guest); 

私はそれだけではありません。

ありがとう、greetz、Malte

答えて

0

これは動作するはずです。ソートとのOrderByはCopmpareTo()メソッドの下

public class Guest : IComparable<Guest> 
    { 
     public bool f { get; set; } 
     public bool g { get; set; } 
     public bool s { get; set; } 
     public string name { get; set; } 

     public int CompareTo(Guest other) 
     { 
      int results = 0; 

      if (this.f) 
      { 
       if (other.f) 
       { 
        results = this.name.CompareTo(other.name); 
       } 
       else 
       { 
        results = 1; 
       } 
      } 
      else 
      { 
       if (other.f) 
       { 
        results = -1; 
       } 
       else 
       { 
        if (this.g) 
        { 
         if (other.g) 
         { 
          results = this.name.CompareTo(other.name); 
         } 
         else 
         { 
          results = 1; 
         } 
        } 
        else 
        { 
         if (other.g) 
         { 
          results = -1; 
         } 
         else 
         { 
          if (this.s) 
          { 
           if (other.s) 
           { 
            results = this.name.CompareTo(other.name); 
           } 
           else 
           { 
            results = 1; 
           } 
          } 
          else 
          { 
           results = this.name.CompareTo(other.name); 
          } 
         } 
        } 
       } 
      } 
      return results; 
     } 

を使用する複数のプロパティがtrueの場合でも動作します簡単な方法です。他のソリューションと同様に1,2,3の代わりに1,2,4を使用しました。 1,2,3は、複数のプロパティが真であるときに3を得る方法が複数あるという問題があります。

public class Guest : IComparable<Guest> 
    { 
     public bool f { get; set; } 
     public bool g { get; set; } 
     public bool s { get; set; } 
     public string name { get; set; } 

     public int CompareTo(Guest other) 
     { 
      int results = 0; 
      int thisRank = (this.f ? 1 : 0) + (this.g ? 2 : 0) + (this.s ? 4 : 0); 
      int otherRank = (other.f ? 1 : 0) + (other.g ? 2 : 0) + (other.s ? 4 : 0); 

      if (thisRank == otherRank) 
      { 
       results = this.name.CompareTo(other.name); 
      } 
      else 
      { 
       results = thisRank.CompareTo(otherRank); 
      } 

      return results; 
     } 
    } 
+0

私は昨晩眠れませんでした。私は数-1を見逃していたことを実感しました。今朝起きて元のコードを修正し、より簡単な方法を追加しました。プロパティに1,2,4を割り当てます。 – jdweng

3

かなり典型的なネストされた並べ替えのような音。グループ化する必要はありません。構文に慣れていない人のために

var result = source 
    .OrderBy(guest => 
    guest.f ? 1 : 
    guest.g ? 2 : 
    guest.s ? 3 : 
    4) 
    .ThenBy(guest => guest.name); 

、私はコードを読むことができます。

  • OrderByの呼び出しでは、連鎖三項演算子を使用して各行のソートキーを生成するラムダ関数があります。 OrderByこのソートキーで並べ替えます。
  • OrderByの結果はIOrderedEnumerable<Guest>であり、ThenByに渡されます。
  • ThenByは、以前の注文操作のソートを保持し、結び目を破るために働きます。
+0

おそらく、これらのネストされたif文がどのように機能するかを説明します。 – Hogan

-1

ここでは、if文のより一般的な構文を使ったDavid Bの例を示します。

var result = source 
      .OrderBy(guest => { if (guest.f == true) return 1 else 
           if (guest.g == true) return 2 else 
           if (guest.s == true) return 3 else return 4;}) 
      .ThenBy(guest => guest.name);