title: Ordering properties in JSON string (Confused with "Avoiding Class properties to arrange in alphabetical order when instantiated")
body: <p>I have a class QuarterlySales which I use in my Services which is consumed by a WebApplication which displays the data on a line chart</p>
  
  <pre><code>public class QuarterlySales
  {
      public string _TimePeriod { get; set; }
      public double _Printer { get; set; }
      public double _Mouse { get; set; }
      public double _KeyBoard { get; set; }
  }
  </code></pre>
  
  <p>The problem is that the Properties are arranged in an alphabetical order when the class is instantiated </p>
  
  <p><a href="https://i.stack.imgur.com/PFFT3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PFFT3.png" alt="sales Object of class QuarterlySales"></a></p>
  
  <p>The "printer" should always be the first in the series of values displayed on chart but is pushed to the last alphabetically, is there a way I can avoid the class arranging the properties in order?</p>
  
  <hr>
  
  <p>Edit 1: 
  Realized its not a problem with Class ordering its properties, but with the JSON object ordered alphabetically that is causing the problem. And its just a co-incidence that both Class properties and JSON results were arranged alphabetically that led to the confusion.</p>
  
  <p>I have assigned Property Order as per @jpgrassi's suggestion</p>
  
  <pre><code>public string QuarterlySalesData()
  {
      DataTable dtSalesData = new DataTable();
      string returnJsonString = string.Empty;
      try
      {
          List&lt;QuarterlySales&gt; salesList = new List&lt;QuarterlySales&gt;();
          dtSalesData = accountObject.GetSalesData();
  
          if (dtSalesData != null)
          {
              for (int i = 0; i &lt; dtSalesData.Rows.Count; i++)
              {
                  QuarterlySales sales = new QuarterlySales();
                  sales._TimePeriod = dtSalesData.Rows[i]["TimePeriod"].ToString();
                  sales._Printer = Convert.ToDouble(dtSalesData.Rows[i]["Item1"].ToString());
                  sales._Mouse = Convert.ToDouble(dtSalesData.Rows[i]["Item2"].ToString());
                  sales._KeyBoard = Convert.ToDouble(dtSalesData.Rows[i]["Item3"].ToString());
                  salesList.Add(sales);
              }
          }
  
          returnJsonString = Newtonsoft.Json.JsonConvert.SerializeObject(salesList, Newtonsoft.Json.Formatting.Indented);
          return returnJsonString;
      }
      catch
      {
          return string.Empty;
      }
  }
  
  public class QuarterlySales
  {
      [JsonProperty(Order = 1)]
      public double _Printer { get; set; }    
  
      [JsonProperty(Order = 2)]
      public double _Mouse { get; set; }
  
      [JsonProperty(Order = 3)]
      public double _KeyBoard { get; set; }
  
      [JsonProperty(Order = 4)]
      public string _TimePeriod { get; set; }    
  }
  </code></pre>
  
  <hr>
  
  <p>Edit 2:
  I was constructing the Series Object and removing underscores from the Data I was passing from Services, but since it was Serialized, I wasn't able to loop it </p>
  
  <pre><code>function ServiceSucceeded(result) {
     dataforChart = result;
     replaceLoop(dataforChart);
     chartSeries = [];
     var parentKey, childKey = [];
     for (parentKey in dataforChart) 
     {
         if (dataforChart.hasOwnProperty(parentKey))
             for (childKey in dataforChart[parentKey]) 
             {
                 if (childKey != "_TimePeriod") 
                 {
                     chartSeries.push(
                     {
                         field: childKey,
                         name: childKey
                     });
                 }
             }
             break;
     }
     createChart();
     $(document).bind("kendo:skinChange", createChart);
  }
  
  function replaceLoop(dataforChart) {
         for (var chartFields = 0; chartFields &lt; dataforChart.length; chartFields++) {
             dataforChart[chartFields]._TimePeriod = dataforChart[chartFields]._TimePeriod.replace(/_/g, ' ');
         }
  </code></pre>
  
  <p>In order to loop, I had to parse the JSON string back to Object</p>
  
  <pre><code>dataforChart = JSON.parse(result);
  </code></pre>
  
  <p>Thank you very much @jpgrassi for guiding and following up with the solution, I have marked your solution as an answer!
  Thanks everyone for comments and pointing out some mistakes which I will surely correct.</p>
  
  <h2>Updated on 02/26/2016</h2>
  
  <p>Instead of all the confusion that the Class alters the properties dynamically or setting the JSON order, I modified the code to generate a dynamic list on the fly which works for any DataTable schema (any number of columns) which doesn't require a custom type (QuarterlySales in this case) to be created. Wanted to update and share.</p>
  
  <pre><code>public string QuarterlySalesData(string chartType)
  {
      DataTable dtPerformanceIndicator;
      string JSONData = string.Empty;
      accountObject.GetSalesData(chartType, out dtPerformanceIndicator);
      var myList = ToDynamicList(dtPerformanceIndicator);
      JSONData = JsonConvert.SerializeObject(myList);
      return JSONData;
  }
  
  private object ToDynamicList(DataTable dttoDynamicList)
  {
      var dynamicList = new List&lt;dynamic&gt;();
      foreach (DataRow dtrow in dttoDynamicList.Rows)
      {
          dynamic dynamicObj = new ExpandoObject();
  
          foreach (DataColumn dtcolumn in dttoDynamicList.Columns)
          {
              var dictionaryofValues = (IDictionary&lt;string, object&gt;)dynamicObj;
              dictionaryofValues[dtcolumn.ColumnName] = dtrow[dtcolumn];
          }
          dynamicList.Add(dictionaryofValues);
      }
      return dynamicList;
  }
  </code></pre>