Sort a queue in C #

Despite the fact that this question sounds like a duplicate, I searched a lot, but did not find a suitable solution.

I have the following classes

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; } } 

flow:

 public class ChangeInfoUploader { static Queue<ChangeInfo> changeInfoQueue = new Queue<ChangeInfo>(); 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(); } } 

Here is an example of the changeInfoQueue queue data.

 <Info TypeofChange="Add" ObjectType="Customer" ObjectId="0005" /> <Info TypeofChange="Add" ObjectType="Customer" ObjectId="0006" /> <Info TypeofChange="Add" ObjectType="Customer" ObjectId="0007" /> <Info TypeofChange="Add" ObjectType="Photo" ObjectId="01a243f5-4894-4d99-8238-9c4cd3" /> 

My question is:

  • I need to sort changeInfoQueue based on ObjectType. How can i do this?

My findings:

  • I found OrderBy . Can i use it? If so, how?

In addition to this, I found priorityQueue. What is the best solution for me?

EDIT:

The values โ€‹โ€‹of this queue are added when the corresponding objects are created. (projects, borders, etc.) and saves it in a local XML file. After that, he needs to write to the database. This is achieved using a stream, and when we save this data, it must be stored in a certain order to avoid foreign key violations. Therefore, this thread is used to call the appropriate methods.

I used orderby as follows:

 Queue<ChangeInfo> changeInfoQueue2 = changeInfoQueue.OrderBy(ChangeInfo => ChangeInfo.ObjectType); 

then it throws the following exception:

It is not possible to implicitly convert the type 'System.Linq.IOrderedEnumerable' to 'System.Collections.Generic.Queue'. Explicit conversion exists (are you skipping listing?)

+4
source share
2 answers

Why do you want to order by type of object in the queue ? The queue, by its definition, is not intended to be ordered in this way, but was intended to work as the first element in the first form.

Either use the List if you want the collection to be ordered, and to arrange the list or create multiple queues for the different types of objects that you have.

For example, if you go to the supermarket, you will have several queues, one for each other section ... it would not make sense to put all the people in one queue, and then โ€œorderโ€ them based on whether they are for a butcher or a bakery.

You have a queue when you need to โ€œqueueโ€ things ... if you are not using the appropriate construct, do not try to put it in the queue. ("if you have a hammer, everything looks like a nail" ... but it should not be)

+14
source

Although all that has been said about using the queue and OrderBy , indeed, you can still order items in your queue.

You can create a new queue based on IOrderedEnumerable :

 Queue<string> queue = new Queue<string>(); queue.Enqueue("first"); queue.Enqueue("second"); queue.Enqueue("third"); queue.Enqueue("fourth"); // Builds a new queue. Items are now alphabetically ordered Queue<string> orderedQueue = new Queue<string>(queue.OrderBy(z => z)); 
+5
source

All Articles