2

我需要在 C# 的同一对象类型中使用 4 个不同的属性来实现排序。

假设学生对象有姓名、身份证、生日和年级。我如何重用代码对它们中的每一个进行排序。我已经设法按名称排序,我如何重用代码?

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

您的第二个错误是因为您不能执行 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 回答
1

自定义排序的标准方法是向IComparer<T>排序算法提供一个。

  • 修改您的排序以接受IComparer<Student>. 通过调用 替换排序中的比较comparer.Compare()。作为奖励,您可以使您的排序通用。

  • IComparer<Student>为您想要对 Student 对象进行排序的每种方式创建一个实现。最简单的方法是使用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 回答