我一直在搜索互联网和书籍,但没有运气,所以希望有人能指出我在正确的方向。按字母顺序排序使用插入排序算法c#
我基本上需要使用插入排序而不是内置方法按字母顺序排列对象的名称。我曾尝试使用数组和列表,但似乎无法使其工作。你会怎么做呢?
我有一类球员,充满了名单的最新尝试对象:
public static List<Player> user = new List<Player>();
private string name; //Read and Write
private int score; //Read and Write
private double health; //Read and Write
private int level; //Read and Write
public string[] inventory = new string[30];
public void setName(String newName)
{
name = newName;
}
public string getName()
{
return name;
}
public void setScore(int newScore)
{
score = newScore;
}
public int getScore()
{
return score;
}
public void setHealth(double newHealth)
{
health = newHealth;
}
public double getHealth()
{
return health;
}
public void setLevel(int newLevel)
{
level = newLevel;
}
public int getLevel()
{
return level;
}
public static void Saved_Player()
{
user.Add(new Player() { name = "Timid Bob", health = 63, level = 6, score = 2000, });
user[0].inventory[0] = "Steel Sword";
user[0].inventory[1] = "1mm MAW";
user[0].inventory[2] = "Short Bow";
user[0].inventory[0] = "Grenade";
user.Add(new Player() {name = "Killer Bob", health = 82, level = 2, score = 1050000, });
user[1].inventory[0] = "Glass Sword";
user[1].inventory[1] = "250mm MAW";
user[1].inventory[2] = "Elephant Bow";
user[1].inventory[3] = "Rock";
等...最多6个用户对象
对它进行排序,我尝试使用下面的代码另一个Form1类:
//须藤代码
for(int i = 0; i < Player.user.Count; i++)
{
while (i index is higher than i+1 index)
{
swap i index with i+1 index
}
}
希望这是正确的:/
我想我明白了PublicJoe的做法,但是如何获取和设置对象的索引?感谢您的期待。
功课?什么不起作用?发布您的代码。 – 2012-02-17 18:49:44
http://www.publicjoe.f9.co.uk/csharp/sort00.html – Josh 2012-02-17 18:57:46
如果您编辑您的问题以包括迄今为止的最佳尝试,并解释为什么您认为它不起作用,您会得到一些帮助它。 – 2012-02-17 19:07:32