title: sort a queue in C#
body: <p>Even though this question sounds as a duplicate, I searched a lot but couldn't find a proper solution.</p>
  
  <p>I have following classes </p>
  
  <pre><code>public enum ChangeType
  { 
      Add,
      Modify,
      Delete
  }
  
  
  
  public enum ChangedObjectType
  { 
      Project,
      Customer,
      Border,
      Photo
  }
  
  public struct ChangeInfo
  {
      public ChangeType typeofChange { get; private set; }
      public ChangedObjectType objectType { get; private set; }
  
      public string objectID { get; private set; }
  
      public ChangeInfo(ChangeType changeType, ChangedObjectType changeObj, string objectId):this()
      {
          typeofChange = changeType;
          objectType = changeObj;
          objectID = objectId;
      }
  
  }
  </code></pre>
  
  <p>thread :</p>
  
  <pre><code>public class ChangeInfoUploader
  { 
      static Queue&lt;ChangeInfo&gt; changeInfoQueue = new Queue&lt;ChangeInfo&gt;();
      static Thread changeInfoUploaderThread = new Thread(new ThreadStart(ChangeInfoUploaderProc));
      static bool isStarted = false;
      static Project currentProject;
  
      public static void Initialize(Project curproject)
      {
          currentProject = curproject;
          isStarted = true;
          changeInfoUploaderThread.Start();
          ResumeData();
      }
  
      static void ChangeInfoUploaderProc()
      {
          while (isStarted)
          {
              if (currentProject != null)
              {
                  ChangeInfo? addToDb = null;
  
               // I need to sort changeInfoQueue before dequeue
                  lock (changeInfoQueue)
                  {
                      if (changeInfoQueue.Count != 0)
                          addToDb = changeInfoQueue.Dequeue();
                  }
              }
          }
          Logdata();
          changeInfoUploaderThread.Abort();
      }
  }
  </code></pre>
  
  <p>here is the sample data of changeInfoQueue queue.</p>
  
  <pre><code>&lt;Info TypeofChange="Add" ObjectType="Customer" ObjectId="0005" /&gt;
  &lt;Info TypeofChange="Add" ObjectType="Customer" ObjectId="0006" /&gt;
  &lt;Info TypeofChange="Add" ObjectType="Customer" ObjectId="0007" /&gt;
  &lt;Info TypeofChange="Add" ObjectType="Photo" ObjectId="01a243f5-4894-4d99-8238-9c4cd3" /&gt;
  </code></pre>
  
  <p>My Question :</p>
  
  <ul>
  <li>I need to sort out changeInfoQueue based on ObjectType. How can i do that?</li>
  </ul>
  
  <p>My findings:</p>
  
  <ul>
  <li>I found <a href="http://msdn.microsoft.com/en-us/library/bb534966%28v=vs.100%29.aspx" rel="nofollow">OrderBy</a> . Is it possible to use it? If so, how?</li>
  </ul>
  
  <p>In addition to that I found priorityQueue. What is the best solution for me?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>The values of this queue are added when relevant objects are created. (projects, borders etc.) and saves it in a local XML file. After that it needs to write to a database. This is accomplished by using a thread and when we save this data it must be saved in particular order to avoid foreign key violations. So this thread is used to call those relevant methods.</p>
  
  <p>I used orderby as follows:  </p>
  
  <pre><code>Queue&lt;ChangeInfo&gt; changeInfoQueue2 = changeInfoQueue.OrderBy(ChangeInfo =&gt; ChangeInfo.ObjectType);
  </code></pre>
  
  <p>then it throws following exception:</p>
  
  <p>Cannot implicitly convert type 'System.Linq.IOrderedEnumerable' to 'System.Collections.Generic.Queue'. An explicit conversion exists (are you missing a cast?)</p>