1

基本的にここでやろうとしていることは、独自の構造体を作成し、ユーザー入力を取得してリストに追加し、さまざまな方法 (ID など) で並べ替えることでそれを利用することです。

構造体を正しく作成したと思いますが、これら 2 つの学生インスタンスを比較し、ID で並べ替え、(ID で並べ替えて) コンソールに出力する方法がわかりません。

何か案は?私は正しい方向に進んでいると思います。

   namespace App26
{
    public struct Student
    {
        public String first, last;
        public double ID;

        public Student(String first, String last, double ID)
        {
            this.first = first;
            this.last = last;
            this.ID = ID;
        }
    }

    class IDCompare : IComparer<Student>
    {
        public int Compare(Student a, Student b)
        {
            return a.first.CompareTo(b.f);
        }
    }


    class Program
    {

        static void Main(string[] args)
        {
            String firstname, lastname, first, last;
            double num, IDnum;

            //First person   
            Console.WriteLine("Please enter first name");
            firstname = Console.ReadLine();
            Console.WriteLine("Please enter last name");
            lastname = Console.ReadLine();
            Console.WriteLine("Please enter ID");
            IDnum = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine();

            //Second Person
            Console.WriteLine("Please enter first name");
            first = Console.ReadLine();
            Console.WriteLine("Please enter last name");
            last = Console.ReadLine();
            Console.WriteLine("Please enter ID");
            num = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine();

            List<Student> list = new List<Student>();
            Student person1 = new Student(firstname, lastname, IDnum);
            //Student person2 = new Student(first, last, num);
            list.Add(person1);
            list.Add(person2);
            list.Sort();

            foreach (Student i in list)
            Console.WriteLine(i);
        }        
       }
}
4

3 に答える 3

2

ソートには以下のコードを使用する必要があります

list.Sort(new IDCompare()); 

また

return a.first.CompareTo(b.f);

間違っているようです。コードのコンパイル時エラーも確認してください。

于 2012-11-20T17:15:33.680 に答える
1

あなたIDCompareはIDを比較しているのではなく、最初に比較しています(私は名前を推測します)。次のように変更する必要があります。

class IDCompare : IComparer<Student>
{
    public int Compare(Student a, Student b)
    {
        return a.ID.CompareTo(b.ID);
    }
}

そして、次のようにソートを呼び出します。

list.Sort(new IDCompare());

IDは通常、倍精度ではなく整数であることに注意してください(ただし、IDの意味はわかりません)。

これを使いたいなら

 foreach (Student i in list)
            Console.WriteLine(i);

構造体のメソッドをオーバーライドすることをお勧めしますToString()(クラスを使用しないのはなぜですか?)

public override string ToString()
{
       return ID.ToString() + " " + first + " " + last + " ";
}
于 2012-11-20T17:28:13.453 に答える
1

Linq も非常に簡単に使用できます。その後、比較子を実装する必要はありません。

foreach (Student entry in myList.OrderBy(student => student.ID))
  Console.Write(entry.Name);

2 番目のプロパティによる並べ替えも利用できます。降順ソートも使用できます。

foreach (Student entry in myList.OrderBy(student => student.ID).ThenBy(student => student.Name))
  Console.Write(entry.Name);
于 2012-11-20T18:17:11.703 に答える