这是我的对象数组
Game[] gameConsoles = new Games[5];
// paramaters are name of the console and game ID
gameConsoles[0] = new Games("Playstation 4", 101);
gameConsoles[1] = new Games("Xbox 1", 108);
gameConsoles[2] = new Games("PS Vita", 110);
gameConsoles[3] = new Games("Wii U", 104);
gameConsoles[4] = new Games("3DS", 102);
for (int i = 0; i < gameConsoles.Length; i++)
{
gameConsoles[i].display();
}
它基本上将在每个消息框中显示所有5个对象,但是我该如何使它能够根据游戏ID的升序显示它们?
我对常规数字数组进行排序时使用的排序算法。
public void ascendingOrder()
{
// helper class
double temp = 0;
for (int j = 0; j < numbers.Length; j++)
{
for (int i = 0; i < numbers.Length - 1; i++)
{
if (numbers[i] > numbers[i + 1])
{
temp = numbers[i];
numbers[i] = numbers[i + 1];
numbers[i + 1] = temp;
}
}
}
}
最佳答案
您可以使用use LINQ OrderBy。
foreach(var item in gameConsole.OrderBy(r=> r.GameID))
{
item.display();
}