Order queue

I have an array of elements sorted so that the oldest element is the first in the array.

I want to load a queue from an array, so when I type in the elements in the queue, the oldest element comes first.

How can i do this?

+1
source share
4 answers

Use LINQ to Objects ...

var q = new Queue<T>(array.OrderBy(d => d.date)); 

EDIT: Ops, the wrong way.

+7
source

If you know that your array is already sorted by old age, you can use:

 Queue<YourType> q = new Queue<YourType>(yourSortedArray); 

If the array is not pre-sorted, you can sort it using LINQ:

 Queue<YourType> q = new Queue<YourType>(yourUnsortedArray.OrderBy(x => x.YourDateProperty)); 

Then you can simply call q.Dequeue to get the items in the oldest and newest order.

+2
source

You want a priority queue . Then it doesn't matter if your incoming items are sorted or not.

There may be an implementation in the library.

PS: the priority in your case will be age-appropriate.

+2
source

try it

 public static T ArrayToQueue<T>(T[] items) { var queue = new Queue<T>(); Array.ForEach(items, i => queue.Enqueue(i)); return queue; } 
+1
source

All Articles