我怎样才能整理出这个容器 [英] How can I sort out this container
本文介绍了我怎样才能整理出这个容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找一个最年轻的玩家,我需要对这个容器进行排序并选择第一个玩家,但是我不知道该怎么做,它说左手边必须是变量属性或索引器。
我尝试了什么:
我试过泡泡排序方法,但它不适合我或者我只是做错了,不知道。
I'm looking for a youngest player, i need to sort this container and take the the first player, however I dont know how to do it, it says "the left hand side must be a variable property or indexer".
What I have tried:
I've tried "bubble Sort" method, yet it did not work for me or maybe im just doing it wrong, no idea.
<pre> public Player FindYoungestPlayer(PlayersContainer entireSportsTeam)
{
Player youngestPlayer = entireSportsTeam.GetPlayer(0);
for (int i = 0; i < entireSportsTeam.Count; i++)
{
for (int j = 0; j < entireSportsTeam.Count-1; j++)
{
if (entireSportsTeam.GetPlayer(i).Birthday > youngestPlayer.Birthday)
{
youngestPlayer = entireSportsTeam.GetPlayer(i);
entireSportsTeam.GetPlayer(i) = entireSportsTeam.GetPlayer(j);
entireSportsTeam.GetPlayer(j) = youngestPlayer;
}
}
}
return youngestPlayer;
}
推荐答案
问题是你没有正确交换,因为你的比较不是你想象的那样是的,即使它匹配,j与你想要交换的东西无关。
如果你想找到最年轻的,那只是一次通过 - 你根本不需要排序或使用嵌套循环:
The problem is that you aren't swapping properly, because your comparison isn't what you think it is, and even when it matches, j is irrelevant to what you want to swap.
If you want to find the youngest, that that's a single pass though - you don't need to sort, or use a nested loop at all:
Player youngestPlayer = entireSportsTeam.GetPlayer(0);
for (int i = 0; i < entireSportsTeam.Count; i++)
{
if (entireSportsTeam.GetPlayer(i).Birthday > youngestPlayer.Birthday)
{
youngestPlayer = entireSportsTeam.GetPlayer(i);
}
}
并且您不应该在名为FindXXX的方法中对任何内容进行排序,因为该名称意味着根本不会对数据进行任何更改。
And you shouldn't sort anything in a method called "FindXXX" because the name implies no changes to data at all.
这篇关于我怎样才能整理出这个容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文