简体   繁体   中英

Sort a list in C#

I have created a list that with 3 different values I want to order the list with one kind of the values. I have done this

      PriorityQueueList.OrderBy(x => x.pQPrioirty).ToList();

but is not odering any thins it just showing me the values according to want I have them in the list

     public class Node
    {
         public int name {get; set;}
         public int pQTime { get; set; }
         public int pQPrioirty { get; set; }

        public Node(int _Name, int _PQ, int _PQP)
       {
        this.name = _Name;
        this.pQTime = _PQ;
        this.pQPrioirty = _PQP;
    }
     }


               List<Node> PriorityQueueList = new List<Node>();

               for (int x = 0; x < nodesNumber; x++)
                {
            Label lblName = new Label();
            NumericUpDown numiNumber = new NumericUpDown();

            xCoor = coor.Next(0, 700);
            yCoor = coor.Next(0, 700);

            if (!randomListxCoor.Contains(xCoor))
            {
                randomListxCoor.Add(xCoor);

            }
            if (!randomListyCoor.Contains(xCoor))
            {
                randomListyCoor.Add(yCoor);

            }

            prioritySaver = pQueuNumbers.Next(1, nodesNumber * 3);

            numiNumber.Name = x.ToString();
            numiNumber.Location = new Point(xCoor, yCoor);
            numiNumber.Size = new Size(50, 15);
            numiNumber.Maximum = 100;
            numiNumber.Minimum = 0;


            lblName.Location = new Point(xCoor + 10, yCoor + 15);
            lblName.Text = x.ToString();
            lblName.ForeColor = System.Drawing.Color.Black;
            lblName.AutoSize = true;

            this.pnlNodes.Controls.Add(lblName);
            this.pnlNodes.Controls.Add(numiNumber);

            numberControls.Add(x, 0);
            waytosave.Add(x, 0);
            savePriority.Add(x, prioritySaver);
            Node Node = new Node(x, 0, prioritySaver);
            this.PriorityQueueList.Add(Node);
                }




      private void RunManualPriorityQueue(int nodesNumber)
    {

        PriorityQueueList.OrderBy(x => x.pQPrioirty).ToList();

            } 

I am getting the values from the numricaUpDown I have done the checking for the change in the values. Everything is just work fine just the sorting it is possible to use PriorityQueueList.OrderBy(x => x.pQPrioirty).ToList(); to sort the list but why it is not sorting according to the priority. I am building the Priority Queue

That method returns an ordered list, which you do not store anywhere. Try

PriorityQueueList=PriorityQueueList.OrderBy(x => x.pQPrioirty).ToList();

You aren't assigning the sorted results back to the list. Try this:

private void RunManualPriorityQueue(int nodesNumber)
{
    PriorityQueueList = PriorityQueueList.OrderBy(x => x.pQPrioirty).ToList();

Or you can do an in-place sort using List<T>.Sort Method (Comparison<T>) which would probably be more efficient.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM