如何消除排序方法中的重复代码-C#

时间:2016-06-22 16:20:16

标签: c#

请帮我消除“SortMG”“SortByName”方法中的重复代码。它基本上是相同的文字,它让我很烦。

class Student
{
    public string name;
    public string major;
    public double grade;
    public string studyForm;


    public Student(string name, string major, double grade, string studyForm)
    {
        this.name = name;
        this.major = major;
        this.grade = grade;
        this.studyForm = studyForm;
    }
}
class Program
{

    static void SortMG(Student[] sortMG, int n)
    {
        int i, j;
        Student tmpMG = new Student("","", 0, "");

        for (i = 0; i < n - 1; i++)
        {
            for (j = i; j < n; j++)
            {
                if (sortMG[j].major.CompareTo(sortMG[i].major)<0)
                {
                   //I'm asking about this part:

                    tmpMG.name = sortMG[j].name;
                    tmpMG.major = sortMG[j].major;
                    tmpMG.studyForm = sortMG[j].studyForm;
                    tmpMG.grade = sortMG[j].grade;

                    sortMG[j].name = sortMG[i].name;
                    sortMG[j].major = sortMG[i].major;
                    sortMG[j].studyForm = sortMG[i].studyForm;
                    sortMG[j].grade = sortMG[i].grade;

                    sortMG[i].name = tmpMG.name;
                    sortMG[i].major = tmpMG.major;
                    sortMG[i].studyForm = tmpMG.studyForm;
                    sortMG[i].grade = tmpMG.grade;
               }
                else if (sortMG[j].major.CompareTo(sortMG[i].major) == 0)
                {
                    if (sortMG[j].grade > sortMG[i].grade)
                    {
                        //and this part:

                        tmpMG.name = sortMG[j].name;
                        tmpMG.major = sortMG[j].major;
                        tmpMG.studyForm = sortMG[j].studyForm;
                        tmpMG.grade = sortMG[j].grade;

                        sortMG[j].name = sortMG[i].name;
                        sortMG[j].major = sortMG[i].major;
                        sortMG[j].studyForm = sortMG[i].studyForm;
                        sortMG[j].grade = sortMG[i].grade;

                        sortMG[i].name = tmpMG.name;
                        sortMG[i].major = tmpMG.major;
                        sortMG[i].studyForm = tmpMG.studyForm;
                        sortMG[i].grade = tmpMG.grade;
                    }
                }
            }
        }
    }
    static void SortByName(Student[] sortN, int n)
    {
        int i, j;
        Student tmpN = new Student("", "", 0, "");

        for (i = 0; i < n - 1; i++)
        {
            for (j = i; j < n; j++)
            {
                if (sortN[j].name.CompareTo(sortN[i].name) < 0)
                {
                    //and this part:

                    tmpN.name = sortN[j].name;
                    tmpN.major = sortN[j].major;
                    tmpN.studyForm = sortN[j].studyForm;
                    tmpN.grade = sortN[j].grade;

                    sortN[j].name = sortN[i].name;
                    sortN[j].major = sortN[i].major;
                    sortN[j].studyForm = sortN[i].studyForm;
                    sortN[j].grade = sortN[i].grade;

                    sortN[i].name = tmpN.name;
                    sortN[i].major = tmpN.major;
                    sortN[i].studyForm = tmpN.studyForm;
                    sortN[i].grade = tmpN.grade;
                }
            }
        }
    }
}

3 个答案:

答案 0 :(得分:1)

看起来您通过交换其属性值来“交换”项目。看起来你应该只是交换项目:

if (sortMG[j].grade > sortMG[i].grade)
{
    //and this part:
    tmpMG = sortMG[j];
    sortMG[j] = sortMG[i];
    sortMG[i] = tmpMG;
}

可以将该交换移动到您从三个位置调用的函数中,以进一步减少重复代码:

public void Swap(Student[] sortMG, int i, int j)
{
     //TODO: add bounds/null hecking
     var tmpMG = sortMG[j];
     sortMG[j] = sortMG[i];
     sortMG[i] = tmpMG; 
}

答案 1 :(得分:1)

使用Linq可以节省很多工作。

例如,您可以使用以下内容对Major Student [] 进行排序:

    List<Student> students = new List<Student>()
    {
        new Student("Jose Mendez", "Math", 80, "Beta"),
        new Student("Alex Bello", "Math", 90, "Alpha"),
        new Student("Bob Junior", "EE", 100, "Charlie")
    };

    Student[] array = students.ToArray();

    array = array.OrderBy(x => x.Major).ToArray();

答案 2 :(得分:0)

好像你要求的是:

    static void copyStudent(Student from, Student to)
    {
        Student tmpMG = new Student();

        tmpMG.name = from.name;
        tmpMG.major = from.major;
        tmpMG.studyForm = from.studyForm;
        tmpMG.grade = from.grade;

        from.name = to.name;
        from.major = to.major;
        from.studyForm = to.studyForm;
        from.grade = to.grade;

        to.name = tmpMG.name;
        to.major = tmpMG.major;
        to.studyForm = tmpMG.studyForm;
        to.grade = tmpMG.grade;
    }

    static void SortMG(Student[] sortMG, int n)
    {
        int i, j;

        for (i = 0; i < n - 1; i++)
        {
            for (j = i; j < n; j++)
            {
                if (sortMG[j].major.CompareTo(sortMG[i].major)<0)
                    copyStudent(sortMG[j], sortMG[i]);
                else if (sortMG[j].major.CompareTo(sortMG[i].major) == 0)
                {
                    if (sortMG[j].grade > sortMG[i].grade)
                        copyStudent(sortMG[j], sortMG[i]);
                }
            }
        }
    }

    static void SortByName(Student[] sortN, int n)
    {
        for (int i = 0; i < n - 1; i++)
            for (int j = i; j < n; j++)
                if (sortN[j].name.CompareTo(sortN[i].name) < 0)
                    copyStudent(sortN[j], sortN[i]);
    }

为什么不使用:

    static void SortMG(Student[] sortMG, int n)
    {
        sortMG = sortMG.OrderBy(i => i.major).ThenBy(i=> i.grade).ToArray();
    }

    static void SortByName(Student[] sortN, int n)
    {
        sortN = sortN.OrderBy(i => i.name).ToArray();
    }