C#_List<T>升序排序和降序排序

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace CShapeTest
{
    class Hero
    {
        private int age;
        private string name;
        private int level;
        private int power;

        public Hero(int age, string name, int level, int power)
        {
            this.age = age;
            this.name = name;
            this.level = level;
            this.power = power;
        }

        public int Age
        {
            get
            {
                return age;
            }
            set
            {
                age = value;
            }
        }

        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }

        public int Level
        {
            get
            {
                return level;
            }
            set
            {
                level = value;
            }
        }

        public int Power
        {
            get
            {
                return power;
            }
            set
            {
                power = value;
            }
        }

        public override string ToString()
        {
            return string.Format("age:{0}, name:{1}, level:{2}, power:{3}", this.age, this.name, this.level, this.power);
        }
    }

    class Start
    {
        static void Main(string[] args)
        {
            List<Hero> personList = new List<Hero>();
            personList.Add(new Hero(25, "cxm", 10, 30));
            personList.Add(new Hero(24, "zqr", 12, 40));
            personList.Add(new Hero(27, "xiaoming", 13, 50));
            personList.Add(new Hero(12, "zhangsan", 52, 10));
            personList.Add(new Hero(20, "lisi", 10, 20));
            personList.Add(new Hero(15, "chenglong", 11, 5));
            personList.Add(new Hero(12, "yunlong", 10, 59));

            // 利用匿名方法对list排序(单权重升序)
            //personList.Sort(delegate(Hero h1, Hero h2) 
            //{
            //    return h1.Age.CompareTo(h2.Age);
            //});

            // 利用Lambda方法对list排序(单权重升序)
            personList.Sort((Hero h1, Hero h2) =>
            {
                return h1.Age.CompareTo(h2.Age);
            });

            foreach (var item in personList)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine("Count:" + personList.Count);

            Console.ReadLine();
        }
    }
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace CShapeTest
{
    class Hero
    {
        private int age;
        private string name;
        private int level;
        private int power;

        public Hero(int age, string name, int level, int power)
        {
            this.age = age;
            this.name = name;
            this.level = level;
            this.power = power;
        }

        public int Age
        {
            get
            {
                return age;
            }
            set
            {
                age = value;
            }
        }

        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }

        public int Level
        {
            get
            {
                return level;
            }
            set
            {
                level = value;
            }
        }

        public int Power
        {
            get
            {
                return power;
            }
            set
            {
                power = value;
            }
        }

        public override string ToString()
        {
            return string.Format("age:{0}, name:{1}, level:{2}, power:{3}", this.age, this.name, this.level, this.power);
        }
    }

    class Start
    {
        static void Main(string[] args)
        {
            List<Hero> personList = new List<Hero>();
            personList.Add(new Hero(25, "cxm", 10, 30));
            personList.Add(new Hero(24, "zqr", 12, 40));
            personList.Add(new Hero(27, "xiaoming", 13, 50));
            personList.Add(new Hero(12, "zhangsan", 52, 10));
            personList.Add(new Hero(20, "lisi", 10, 20));
            personList.Add(new Hero(15, "chenglong", 11, 5));
            personList.Add(new Hero(12, "yunlong", 10, 59));

            // 利用匿名方法对list排序(多权重升序)
            //personList.Sort(delegate(Hero h1, Hero h2) 
            //{
            //    if (h1.Age < h2.Age)
            //    {
            //        return -1;//左值小于右值,返回-1,为升序,如果返回1,就是降序
            //    }
            //    else if (h1.Age == h2.Age)//左值等于右值,返回0
            //    {
            //        if (h1.Level < h2.Level)
            //        {
            //            return -1;
            //        }
            //        else if (h1.Level == h2.Level)
            //        {
            //            return 0;
            //        }
            //        else
            //        {
            //            return 1;
            //        }
            //    }
            //    else
            //    {
            //        return 1;//左值大于右值,返回1,为升序,如果返回-1,就是降序
            //    }
            //});

            // 利用Lambda方法对list排序(多权重升序)
            personList.Sort((Hero h1, Hero h2) =>
            {
                if (h1.Age < h2.Age)
                {
                    return -1;//左值小于右值,返回-1,为升序,如果返回1,就是降序
                }
                else if (h1.Age == h2.Age)//左值等于右值,返回0
                {
                    if (h1.Level < h2.Level)
                    {
                        return -1;
                    }
                    else if (h1.Level == h2.Level)
                    {
                        return 0;
                    }
                    else
                    {
                        return 1;
                    }
                }
                else
                {
                    return 1;//左值大于右值,返回1,为升序,如果返回-1,就是降序
                }
            });

            foreach (var item in personList)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine("Count:" + personList.Count);

            Console.ReadLine();
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值