day09 c# ArrayList的用法

using System;
using System.Collections;
namespace day09
{
    public class Student
    {
        public string name;
        public int age;
        public float height;
        public string sex;
        public long stuNumber;
        public float math;
        public float chinses;
        public float english;

        public Student(string name, int age, float height, string sex, long stuNumber, float math, float chinses, float english)
        {
            this.name = name;
            this.age = age;
            this.height = height;
            this.sex = sex;
            this.stuNumber = stuNumber;
            this.math = math;
            this.chinses = chinses;
            this.english = english;
        }
    }
    public class SortByHeight : IComparer
    {  
        public int Compare(object x, object y)
        {
            Student student01 = x as Student;
            Student student02 = y as Student;
            if (student01 == null)
            {
                return 1;
            }
            if (student02 == null)
            {
                return -1;
            }
            float a = student01.height;
            float b = student02.height;
            if (a > b)
            {
                //前 9,8,7,6,5 後
                return -1;
            }
            else if (a < b)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
    }

    public class SortByStuNUmber : IComparer
    {
        public int Compare(object x, object y)
        {
            Student student01 = x as Student;
            Student student02 = y as Student;
            if (student01 == null)
            {
                return 1;
            }
            if (student02 == null)
            {
                return -1;
            }
            long a = student01.stuNumber;
            long b = student02.stuNumber;
            if (a > b)
            {
                //前 9,8,7,6,5 後
                return 1;
            }
            else if (a < b)
            {
                return -1;
            }
            else
            {
                return 0;
            }
        }
    }

    public class SortByFenShu : IComparer
    {
        public int Compare(object x, object y)
        {
            Student student01 = x as Student;
            Student student02 = y as Student;
            if (student01 == null)
            {
                return 1;
            }
            if (student02 == null)
            {
                return -1;
            }
            float a = student01.chinses;
            float b = student02.chinses;
            float c = student01.math;
            float d = student02.math;
            float e = student01.english;
            float f = student02.english;
            if (c > d)                                 //先比數學     
            {
                return -1;
            }
            else if (c < d)
            {
                return 1;
            }
            else if(c==d)                             //數學相同比語文
            {
                if (a > b)
                {
                    return -1;
                }
                else if (a < b)
                {
                    return 1;
                }
                else if (a == b)                      //語文相同比英語
                {
                    if (e > f)
                    {
                        return -1;
                    }
                    else if (e <f)
                    {
                        return 1;
                    }
                    else if (e == f)
                    {
                        return 0;
                    }
                }
            }
            else
            { 
                return 0; 
            }
            return 0;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {

            Student student00 = new Student("小0", 20, 1.65f, "男", 1001, 99, 55, 66);
            Student student01 = new Student("小1", 20, 1.67f, "男", 1002, 68, 45, 56);
            Student student02 = new Student("小2", 20, 1.88f, "女", 1003, 39, 65, 66);
            Student student03 = new Student("小3", 20, 1.55f, "男", 1004, 29, 75, 76);
            Student student04 = new Student("小4", 20, 1.34f, "男", 1005, 68, 55, 49);
            Student student05 = new Student("小5", 20, 1.65f, "女", 1006, 49, 55, 38);
            Student student06 = new Student("小6", 20, 1.97f, "女", 1007, 59, 55, 66);
            Student student07 = new Student("小7", 20, 1.56f, "男", 1008, 69, 69, 96);
            Student student08 = new Student("小8", 20, 1.43f, "男", 1009, 88, 53, 99);
            Student student09 = new Student("小9", 20, 1.38f, "女", 1010, 66, 59, 100);
            ArrayList array = new ArrayList();

            array.Add(student00);
            array.Add(student01);
            array.Add(student02);
            array.Add(2);
            array.Add(student03);
            array.Add(student04);
            array.Add(student05);
            array.Add(student06);
            array.Add(student07);
            array.Add(student08);
            array.Add(student09);       
            array.Sort(new SortByHeight()) ;
            Console.WriteLine("按身高排序");
            foreach (var item in array)
            {
                if (item is Student)
                {
                    Student student = (Student)item;
                    Console.WriteLine($"{student.name}年齡{student.age}身高{student.height}性別{student.sex}學號{student.stuNumber}數學{student.math}語文" +
                        $"{student.chinses}英語{student.english} ");
                }
            }
            Console.WriteLine("===========================");
            array.Sort(new SortByStuNUmber());
            Console.WriteLine("按學號排序");
            foreach (var item in array)
            {
                if (item is Student)
                {
                    Student student = (Student)item;
                    Console.WriteLine($"{student.name}年齡{student.age}身高{student.height}性別{student.sex}學號{student.stuNumber}數學{student.math}語文" +
                        $"{student.chinses}英語{student.english} ");
                }
            }
            Console.WriteLine("===========================");
            array.Sort(new SortByFenShu());
            Console.WriteLine("按成績排序");
            foreach (var item in array)
            {
                if (item is Student)
                {
                    Student student = (Student)item;
                    Console.WriteLine($"{student.name}年齡{student.age}身高{student.height}" +
                        $"性別{student.sex}學號{student.stuNumber}數學{student.math}語文" +
                        $"{student.chinses}英語{student.english} ");
                }
            }
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章