学习记录:冒泡排序和IComparable<Student>接口

今天是学习C#第17天。。。
今天没有学习新东西,在做项目,但昨天昨天在做作业时用到了一个很好用的排序接口IComparable<Student>。
说它之前,先回忆一下冒泡排序。


冒泡排序:

两个变量或两个数组元素相互交换数据:

int a = 10, b = 20;         
//将a值赋给中间变量
int temp = a;//a最开始时的值:10
a = b;
b = temp;
        
//赋值给临时变量
temp = numbers[0];//1
numbers[0] = numbers[3];//-1
numbers[3] = temp;//1

排序:

        bool complete;
      
        //轮次 == 数组长度 - 1
        for (int i = 0; i < numbers.Length - 1; i++)
        {
            complete = true;
            
            //比较的次数
            for (int j = 0; j < numbers.Length - i - 1; j++)
            {
                //从小到大
                // if(numbers[j] > numbers[j+1])
                //从大到小
                // if(numbers[j] < numbers[j+1])
                
                //前大后小,交换
                if (numbers[j] > numbers[j + 1])
                {
                    //排序还没有完成
                    complete = false;
                    //临时存储
                    int temp = numbers[j];
                    numbers[j] = numbers[j + 1];
                    numbers[j + 1] = temp;
                }
            }
            
            //如果已经完成了排序,则跳出外层循环,结束排序
            if(complete)
                break;
        }

冒泡排序是最简单的排序方法,可以通过两两比较进行排序


IComparable<Student>接口:

定义由值类型或类实现的通用比较方法,旨在创建特定于类型的比较方法以对实例进行排序。

使用IComparable<Student>接口我们可以很快实现排序

    public class Student : IComparable<Student>
    {
        public int CompareTo(Student other)
        {
        }
    }

CompareTo返回值int的含义:

“值” 含义
小于零 此对象 CompareTo 在排序顺序中位于方法所指定的对象之前。
此当前实例在排序顺序中与方法参数指定的对象出现在同一位置 CompareTo
大于零 此当前实例 CompareTo 位于排序顺序中由方法自变量指定的对象之后。

 /// <summary>
/// 科目枚举
/// </summary>
 public enum SubjectType
{
    Math,
    Chinese,
    English

}


public class Student : IComparable<Student>
    {
        public string name;
        public char sex;
        public byte age;
        public int studentNumber;

        /// <summary>
        /// 地址
        /// </summary>
        public string site;

        /// <summary>
        /// 成绩<科目,分数>
        /// </summary>
        public Dictionary<SubjectType, byte> scores;


        public Student(string name, char sex, byte age, int studentNumber, string site)
        {
            this.name = name;
            this.sex = sex;
            this.age = age;
            this.studentNumber = studentNumber;
            this.site = site;
            scores = new Dictionary<SubjectType, byte>();
        }

        public void AddScore(SubjectType subjectType, byte score)
        {
            scores.Add(subjectType, score);
        }




        public int CompareTo(Student other)
        {
           //根据年龄进行排序 ,升序排序,大于other对象返回1,将其放在other对象的后面
            if (this.age > other.age)
            {
                return 1;
            }
            else if (this.age < other.age)
            {
                return -1;
            }
            else
            {
                return 0;
            }



            //根据成绩排序,数学一样就比语文,语文还一样就比英语
            // foreach (var item in scores)
            // {
            //   if (item.Value > other.scores[item.Key])
            //    {
            //       return -1;
            //    }
            //  else if (item.Value < other.scores[item.Key])
            //   {
            //       return 1;
            //   }
            // }

            // return 0;
        }
         
 }

进行比较后,可以通过Sort()排序

例:

  List<Student> students = new List<Student>(Students.Values);
  Students .Sort();
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容