调用.OrderBy后,生成的List不会进行排序



我有一个名为ShipPosition的类,它看起来像这样:

public int nodesHit = 0;
PositionNode currentNode;

我想要一个名为shipPositions的飞船列表,按命中节点最多到最少的顺序排列。我已经在一个名为allShips的变量中列出了我想要订购的所有船只的列表,因此,为此,我在以下代码行中使用了.OrderBy((:

List<ShipPosition> shipPositions = allShips.OrderByDescending(ship => ship.nodesHit).ToList();

然而shipPositions与所有船舶保持相同。

你能告诉我我缺了什么吗?

似乎对我来说还可以这是小提琴https://dotnetfiddle.net/5oQO3B

using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
public static void Main()
{
List<Ship> ships= new List<Ship>();
ships.Add(new Ship{NodesHit = 5});
ships.Add(new Ship{NodesHit = 10});   
ships.Add(new Ship{NodesHit = 3});
ships.Add(new Ship{NodesHit = 9});
ships.Add(new Ship{NodesHit = 1});

foreach (Ship ship in ships.OrderByDescending(ship => ship.NodesHit))
{
Console.WriteLine(ship.NodesHit.ToString());
}       
}
}
public class  Ship{
public int NodesHit {get; set;}
}

最新更新