How to order properties within an object
<p>If I have a class that looks like the model below, such that there are several integer properties <code>Cand1Votes</code> , <code>Cand2Votes</code> and <code>Cand3Votes</code> that needs to be displayed in <code>DescendingOrder</code> in the view for each <code>Election</code> object. For a particular election, <code>Cand1Votes</code> may be first while <code>Cand3Votes</code> second and <code>Cand2Votes</code> third.</p>

<p>My solution so far is to create a <code>List&lt;KeyValuePair&lt;string, int?&gt;&gt;</code> for each of the election objects then send that to the ViewModel and use that in the View.</p>

<p>My solution so far <em>works</em>, but I'm looking for a solution whereby I can dynamically order the integer elements within the object, without having to create another <code>List&lt;KeyValuePair&lt;string, int?&gt;&gt;</code>.</p>

<p>Any help will be appreciated.</p>

<p>Model</p>

<pre><code>public class Elections{
    public int Id           { get; set; }
    public int? TotalVotes  { get; set; }
    public int? Cand1Votes  { get; set; }
    public string Cand2Name { get; set; }
    public int? Cand2Votes  { get; set; }
    public string Cand2Name { get; set; }
    public string Cand3Name { get; set; }
    public int? Cand3Votes  { get; set; }
    public int? OtherVotes  { get; set; }
}

 public static List&lt;KeyValuePair&lt;string, int?&gt;&gt; OrderByVotes(Election election)
 {
     var votesArray = new List&lt;KeyValuePair&lt;string, int?&gt;&gt;()
     {
         new KeyValuePair&lt;string, int?&gt;(election.Cand1Name, election.Cand1Votes),
         new KeyValuePair&lt;string, int?&gt;(election.Cand2Name, election.Cand2Votes),
         new KeyValuePair&lt;string, int?&gt;(election.Cand3Name, election.Cand3Votes),
         new KeyValuePair&lt;string, int?&gt;("Other Candidates", election.OtherVotes)
     };
     var result = votesArray.OrderByDescending(k =&gt; k.Value);
     return result.ToList();
 }
</code></pre>