Click here to Skip to main content
15,890,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,I know that the principle of the queue is first in first out, but I want arrange this queue decreasing how can do this?
C#
Queue qu = new Queue();
qu.Enqueue(4);
qu.Enqueue(1);
qu.Enqueue(5);
qu.Enqueue(2);
qu.Enqueue(3);
Posted
Comments
Tomas Takac 5-Feb-15 13:41pm    
Why? Do you want to keep the items sorted in the queue?
[no name] 5-Feb-15 14:03pm    
yes, sorting in the same queue

You'r right, it is not for that. If you want to sort, use a List instead.

C#
Queue<string> numbers = new Queue<string>();
        numbers.Enqueue("one");
        numbers.Enqueue("two");
        numbers.Enqueue("three");
        numbers.Enqueue("four");
        numbers.Enqueue("five");
		
var a = numbers.ToList();
a.Sort();
a.Dump();

/*numbers.Clear();
a.ForEach((x) => {numbers.Enqueue(x);});*/

numbers = new Queue<string>(a);

numbers.Dump();

Non-generic way:
C#
Queue qu = new Queue();
        qu.Enqueue(4);
        qu.Enqueue(1);
        qu.Enqueue(5);
        qu.Enqueue(2);
        qu.Enqueue(3);
		
var a = qu.ToArray().ToList();
a.Sort();
a.Dump();

qu = new Queue(a);

qu.Dump();
 
Share this answer
 
v3
Comments
[no name] 5-Feb-15 14:05pm    
thank you, can you explain this and why you use Dump();
Zoltán Zörgő 5-Feb-15 14:08pm    
Sorry, this is a snippet, which can be run in LinqPad. .Dump() is an extension that dumps the list of any object to the screen. You don't need it in code.
PIEBALDconsult 5-Feb-15 14:09pm    
numbers = new Queue<string>(a)
Zoltán Zörgő 5-Feb-15 14:15pm    
Not exactly, but right.
PIEBALDconsult 5-Feb-15 14:20pm    
Generic parameter got mangled.
Find or implement a priority queue or uses a sorted list.

A .NET queue is not intended for your scenario. This class is designed for a specific purpose (first in first out).
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900