Click here to Skip to main content
15,889,767 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,I know that the principle of the queue is first in first out,but I want to put the number in queue in array not using ToArray() I want to use like double[] a = new double[100]; and ordering by descending and put the first smallest number in a variable how can do this??

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(); 
qu = new Queue(a);

in this code using qu.ToArray() I want to use array like this
C#
double[] a = new double[100]
Posted
Comments
Sergey Alexandrovich Kryukov 21-Feb-15 6:05am    
Why sorting a queue? You can use more appropriate collection...
—SA
[no name] 21-Feb-15 6:41am    
I have queue consist of number of element I need to build method to sort queue ..in every time put element in queue and choose the smallest one to put it in a variable
Sergey Alexandrovich Kryukov 21-Feb-15 12:30pm    
This is not called "queue". You have to choose some other collection type; it will also depend on other requirement.
—SA
BillWoodruff 21-Feb-15 14:02pm    
Your code and this post (and the comments you make to OriginalGriff in response to his answer) lead me to think you are not clear about what you want to do. The purpose of a Queue is to maintain the order in which objects were added to it; it makes no sense to sort it.

Try and step-back and rethink what your goal is, and make a clear statement in your original post of the goal.

1 solution

Reordering a queue is not a normal operation - if you are doing that, then you are probably using the wrong collection.

And quite why you don't want to use ToArray I'm not sure - but you can't just cast a collection to an array in the way you seem to think: that is why ToArray exists.

You don't have to convert anything to an array in order to sort it though:
C#
Queue<int> qu = new Queue<int>();
qu.Enqueue(4);
qu.Enqueue(1);
qu.Enqueue(5);
qu.Enqueue(2);
qu.Enqueue(3);
qu = new Queue<int>(qu.OrderBy(q => q));
Will do it.
 
Share this answer
 
Comments
[no name] 21-Feb-15 3:49am    
@OriginalGriff ,Okay if I use ToArray I want to put the smallest number in a variable like c=smallest number how can do this
OriginalGriff 21-Feb-15 4:03am    
You don't need to use ToArray or sort anything to get the smallest value!
int min = qu.Min();
will do it.
[no name] 21-Feb-15 6:31am    
No,I need to put queue in array and then sort array and put the last one (smallest number ) in variable,if I have 2 or more the same value in this case(int min = qu.Min()) how can choose the last one
OriginalGriff 21-Feb-15 6:49am    
"if I have 2 or more the same value" If they are the same value, does it matter which you choose?
3, 7, 2, 4, 2, 6
min == 2 -- it doesn't matter if that was the first "2" or the second "2".

Sorting is an expensive business (as is converting a Queue to an array): if you want to find a single minimum only then you are better using a loop-based method instead (either foreach, for or Linq).
[no name] 21-Feb-15 7:48am    
I,m work on project and I need to choose the last one ,when choose any value this cause problem to me .

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