class Program
{
public class Pokemon
{
public string Name;
public int Weight;
}
public class myReverserClass : IComparer
{
// Calls CaseInsensitiveComparer.Compare with the parameters reversed.
int IComparer.Compare(Object x, Object y)
{
return ((new CaseInsensitiveComparer()).Compare(x, y));
}
}
static void Main(string[] args)
{
IComparer myComparer = new myReverserClass();
Random r = new Random();
string[] pokemons = new string[10];
pokemons[0] = "Bulbasaur";
pokemons[1] = "Charmander";
pokemons[2] = "Squirtle";
pokemons[3] = "Groulit";
pokemons[4] = "Caterpie";
pokemons[5] = "Weedle";
pokemons[6] = "Kakuna";
pokemons[7] = "Beedrill";
pokemons[8] = "Pidgey";
pokemons[9] = "Rattata";
string[] sovpad = new string[10];
Pokemon[] pokemon = new Pokemon[10];
for (int i = 0; i < pokemons.Length; i++)
{
string sovpadtrue;
sovpadtrue = pokemons[r.Next(0, pokemons.Length)];
for(int p = 0; p<sovpad.Length;p++)
{
if(sovpadtrue == sovpad[p])
{
sovpadtrue = pokemons[r.Next(0, pokemons.Length)];
p = -1;
}
}
sovpad[i] = sovpadtrue;
pokemon[i] = new Pokemon();
pokemon[i].Name = sovpadtrue;
pokemon[i].Weight = r.Next(10, 1000);
}
Array.Sort(pokemon,myComparer);
Console.ReadLine();
}
}
如何添加一个类 myReverserClass,对一组口袋妖怪进行排序,首先按名称,然后按重量?错误:至少有一个对象必须实现接口 IComparable。仅对一维数组的 Msdn 示例。而 myReverserClass 取自 msdn。