• Sort 排序


    实体类

    public class Person
        {
            public string Name { get; set; }
            public int Age { get; set; }
            public Person(string name, int age)
            {
                Name = name;
                Age = age;
            }
        }

    排序

        List<Person> personArr = new List<Person>();
        personArr.Add(new Person("BAIQJH", 18));
        personArr.Add(new Person("EAIGG", 20));
        personArr.Add(new Person("AAICC", 19));
        //按名字排序
        personArr.Sort(new Comparison<Person>(delegate(Person p1, Person p2) { return p1.Name.CompareTo(p2.Name); }));
        //按年龄排序
        personArr.Sort(new Comparison<Person>(delegate(Person p1, Person p2) { return p1.Age - p2.Age; }));
        //显示结果
        foreach (var item in personArr)
        {
            textBox1.Text += item.Name + " " + item.Age + Environment.NewLine;
        }

    结果

    //按名字排序结果
    AAICC 19
    BAIQJH 18
    EAIGG 20
    
    //按年龄排序结果
    BAIQJH 18
    AAICC 19
    EAIGG 20
  • 相关阅读:
    ZOJ4125 Sekiro
    ZOJ4118 Stones in the Bucket
    ZOJ4115 Wandering Robot
    ZOJ4113 Calandar
    【递归】N皇后问题 和 2n皇后问题 dfs
    7-18
    7_13
    二维前缀和
    64位整数乘法
    【分治】魔法石的诱惑
  • 原文地址:https://www.cnblogs.com/baiqjh/p/2768987.html
Copyright © 2020-2023  润新知