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 を追加して、最初に名前で、次に重みでポケモンの配列をソートする方法は? エラー: 少なくとも 1 つのオブジェクトでインターフェイス IComparable を実装する必要があります。1次元配列のみのMsdnの例。そして、msdn から取得した myReverserClass 。