2

C# の同じオブジェクト型で 4 つの異なる属性を使用して並べ替えを実装する必要があります。

オブジェクト Student に名前、ID、生年月日、学年があるとしましょう。それぞれをソートするためにコードを再利用するにはどうすればよいですか。名前でソートできましたが、コードを再利用するにはどうすればよいですか?

private void btnSortName_Click(object sender, EventArgs e)
    {
        Student obj = new Student();
        List<Student> listOfStudents = obj.List();
        int student_count = listOfStudents.Count();
        int first_index, last_index;

        for (first_index = 1; first_index < student_count; first_index++)
        {
            last_index = first_index - 1;
            Student first = listOfStudents[first_index];
            Student last = listOfStudents[last_index];

            while (last_index >= 0 && DateTime.Compare(last.RDate, first.RDate) > 0)
            {
                listOfStudents[last_index + 1] = listOfStudents[last_index];
                last_index = last_index - 1;
            }
            listOfStudents[last_index + 1] = first;
        }
        DataTable dt = Utility.ConvertToDataTable(listOfStudents);
        dataGridStudents.DataSource = dt;
        btnSortName.Visible = false;
        btnSortName.Enabled = false;
        btnSortNameD.Visible = true;
        btnSortNameD.Enabled = true;
    }

挿入ソートのメソッドを作成し、属性をパラメーターとして渡し、そのオブジェクトのリストを返すことでこれを実行しようとしましたが、これらは両方ともエラーを示しています:

public List<Student> insertion_Sort(ref String data, Boolean asc)
    {
        Student obj = new Student();
        List<Student> listOfStudents = obj.List();
        int student_count = listOfStudents.Count();
        int first_index, last_index;

        for (first_index = 1; first_index < student_count; first_index++)
        {
            last_index = first_index - 1;
            Student first = listOfStudents[first_index];
            Student last = listOfStudents[last_index];

            if (asc){
                while (last_index >= 0 && DateTime.Compare(last.data, first.data) > 0)
                {
                    listOfStudents[last_index + 1] = listOfStudents[last_index];
                    last_index = last_index - 1;
                }
                listOfStudents[last_index + 1] = first;
            }
            else
            {
                while (last_index >= 0 && DateTime.Compare(last.data, first.data) < 0)
                {
                    listOfStudents[last_index + 1] = listOfStudents[last_index];
                    last_index = last_index - 1;
                }
                listOfStudents[last_index + 1] = first;
            }
        }
        return listOfStudents;
    }

戻り値の種類

パラメータとしての属性

これらの問題を解決するにはどうすればよいですか?

4

2 に答える 2

1

ソートをカスタマイズする標準的な方法はIComparer<T>、ソート アルゴリズムに を指定することです。

  • を受け入れるように並べ替えを変更しますIComparer<Student>。の呼び出しによる並べ替えの比較を置き換えますcomparer.Compare()。おまけとして、並べ替えをジェネリックにすることができます。

  • IComparer<Student>Student オブジェクトをソートする方法ごとに forの実装を作成します。これを行う最も簡単な方法は、次を使用することComparer.Create()です。

    var nameComparer = Comparer<Student>.Create((studentA, studentB) => string.Compare(studentA.Name, studentB.Name));
    var gradeComparer = Comparer<Student>.Create((studentA, studentB) => studentA.grade.CompareTo(studentB.grade));
    

並べ替えをカスタマイズする方法List<T>.Sort()と許可するのと同じです。Array.Sort()

于 2020-01-03T05:32:38.747 に答える
1

2番目のエラーは、属性が属性名の文字列であるときに obj.attribute を実行できないためです。名前で属性を取得するようなものを使用obj.GetType().GetProperty(propertyName).GetValue(obj, null)します..

たぶんこれでうまくいくでしょう。チェックする学生データや ac# コンパイラが今のところないので試してみてください。

public List<Student> insertion_Sort(List<Student> listOfStudents,ref String propertyName, Boolean asc)
    {
        Student obj = listOfStudents [0];
        int student_count = listOfStudents.Count();
        int first_index, last_index;
        dynamic prop= obj.GetType().GetProperty(propertyName);
        for (first_index = 1; first_index < student_count; first_index++)
        {
            last_index = first_index - 1;
            Student first = listOfStudents[first_index];
            Student last = listOfStudents[last_index];

            if (asc){
                while (last_index >= 0 && prop.GetValue(first, null)-prop.GetValue(last, null) > 0)
                {
                    listOfStudents[last_index + 1] = listOfStudents[last_index];
                    last_index = last_index - 1;
                }
                listOfStudents[last_index + 1] = first;
            }
            else
            {
                while (last_index >= 0 && prop.GetValue(first, null)-prop.GetValue(last, null) <0)
                {
                    listOfStudents[last_index + 1] = listOfStudents[last_index];
                    last_index = last_index - 1;
                }
                listOfStudents[last_index + 1] = first;
            }
        }
        return listOfStudents;
    }
于 2020-01-03T05:01:54.740 に答える