title: Order properties of object by value of customAttribute
body: <p>What I'm trying to do is wirte linq expression which allows me to order my <code>List&lt;PropertyInfo&gt;</code> of some object by Custom attribute for example:</p>
  
  <pre><code>public class SampleClass{
  
     [CustomAttribute("MyAttrib1",1)]
     public string Name{ get; set; }
     [CustomAttribute("MyAttrib2",1)]
     public string Desc{get;set;}
     [CustomAttribute("MyAttrib1",2)]
     public int Price{get;set;}
  }
  </code></pre>
  
  <p>CustomAttribute.cs:</p>
  
  <pre><code>public class CustomAttribute: Attribute{
      public string AttribName{get;set;}
      public int Index{get;set;}
      public CustomAttribute(string attribName,int index)
      {
          AttribName = attribName;
          Index = index;
      }
  }
  </code></pre>
  
  <p>so far I was able to get all properties from my class named SampleClass:</p>
  
  <pre><code>List&lt;PropertyInfo&gt; propertiesList = new List&lt;PropertyInfo&gt;((IEnumerable&lt;PropertyInfo&gt;)typeof(SampleClass).GetProperties());
  </code></pre>
  
  <p>my tryed so far to sort this <code>propertiesList</code> (which btw doesn't work) :</p>
  
  <pre><code>var sortedPropertys = propertiesList
              .OrderByDescending(
                  (x, y) =&gt; ((CustomAttribute) Attribute.GetCustomAttribute((PropertyInfo) x, typeof (CustomAttribute))).AttribName 
                  .CompareTo((((CustomAttribute) Attribute.GetCustomAttribute((PropertyInfo) y, typeof (CustomAttribute))).AttribName ))
              ).OrderByDescending(
                  (x,y)=&gt;((CustomAttribute) Attribute.GetCustomAttribute((PropertyInfo) x, typeof (CustomAttribute))).Index
                  .CompareTo((((CustomAttribute) Attribute.GetCustomAttribute((PropertyInfo) y, typeof (CustomAttribute))).Index)))
                  .Select(x=&gt;x);
  </code></pre>
  
  <p><strong>Output List should be (I'll tell it only with PropertyInfo.Name):</strong></p>
  
  <pre><code>property name: Name,Price,Desc
  </code></pre>
  
  <p>My question is: <strong>Is it possible to do that? And If yes how can I do this properly?</strong></p>
  
  <p>If you have some question pls ask (I'll do my best to answer every uncertainties). I hope description of problem is enough.</p>
  
  <p>Thanks for advance:)</p>