Unity基础之C#进阶篇笔记5:List排序

List自带排序方法

            List<int> list = new List<int>();
            list.Add(3);
            list.Add(2);
            list.Add(6);
            list.Add(1);
            list.Add(4);
            list.Add(5);
            for (int i = 0; i < list.Count; i++)
            {
                Console.WriteLine(list[i]);
            }
            //list提供了排序方法
            list.Sort();
            Console.WriteLine("**************");
            for (int i = 0; i < list.Count; i++)
            {
                Console.WriteLine(list[i]);
            }
            //ArrayList中也有Sort排序方法

自定义类的排序

    class Item : IComparable<Item>
    {
        public int money;

        public Item(int money)
        {
            this.money = money;
        }

        public int CompareTo(Item other)
        {
            //返回值的含义
            //小于0:
            //放在传入对象的前面
            //等于0:
            //保持当前的位置不变
            //大于0:
            //放在传入对象的后面

            //可以简单理解 传入对象的位置 就是0
            //如果你的返回为负数 就放在它的左边 也就前面
            //如果你返回正数 就放在它的右边 也就是后面

            if( this.money > other.money )
            {
                return -1;
            }
            else
            {
                return 1;
            }
        }
    }
           List<Item> itemList = new List<Item>();
            itemList.Add(new Item(45));
            itemList.Add(new Item(10));
            itemList.Add(new Item(99));
            itemList.Add(new Item(24));
            itemList.Add(new Item(100));
            itemList.Add(new Item(12));
            //排序方法
            itemList.Sort();
            for (int i = 0; i < itemList.Count; i++)
            {
                Console.WriteLine(itemList[i].money);
            }

通过委托函数进行排序

    class ShopItem
    {
        public int id;

        public ShopItem(int id)
        {
            this.id = id;
        }
    }
            List<ShopItem> shopItems = new List<ShopItem>();
            shopItems.Add(new ShopItem(2));
            shopItems.Add(new ShopItem(1));
            shopItems.Add(new ShopItem(4));
            shopItems.Add(new ShopItem(3));
            shopItems.Add(new ShopItem(6));
            shopItems.Add(new ShopItem(5));

            //shopItems.Sort(SortShopItem);
            //匿名函数
            //shopItems.Sort(delegate (ShopItem a, ShopItem b)
            //{
            //    if (a.id > b.id)
            //    {
            //        return -1;
            //    }
            //    else
            //    {
            //        return 1;
            //    }
            //});
            //lambad表达式 配合 三目运算符的 完美呈现
            shopItems.Sort((a, b) =>{ return a.id > b.id ? 1 : -1;});

            Console.WriteLine("*********************");
            for (int i = 0; i < shopItems.Count; i++)
            {
                Console.WriteLine(shopItems[i].id);
            }
            #endregion
        }

        static int SortShopItem( ShopItem a, ShopItem b )
        {
            //传入的两个对象 为列表中的两个对象
            //进行两两的比较  用左边的和右边的条件 比较
            //返回值规则 和之前一样 0做标准 负数在左(前) 正数在右(后)
            if (a.id > b.id)
            {
                return -1;
            }
            else
            {
                return 1;
            }
        }

总结

  系统自带的变量(int, float, double.....) 一般都可以直接Sort
  自定义类SOrt有两种方式
    1.继承接口 IComparable
    2.在Sort中传入委托函数

练习题

1.练习题1

    //写一个怪物类,创建10个怪物将其添加到List中
    //对List列表进行排序,根据用户输入数字进行排序
    //1、攻击排序
    //2、防御排序
    //3、血量排序
    //4、反转
    class Monster
    {
        public static int SortType = 1;

        public int hp;
        public int atk;
        public int def;
        public Monster(int hp, int atk, int def)
        {
            this.hp = hp;
            this.atk = atk;
            this.def = def;
        }

        public override string ToString()
        {
            return string.Format("怪物信息-血量{0}攻击力{1}防御力{2}", this.hp, this.atk, this.def);
        }
    }
     class Program
    {
        static void Main(string[] args)
        {
        	
            List<Monster> monsters = new List<Monster>();
            Random r = new Random();
            for (int i = 0; i < 10; i++)
            {
                monsters.Add(new Monster(r.Next(100, 201), r.Next(5, 21), r.Next(2, 10)));
                Console.WriteLine(monsters[i]);
            }

            Console.WriteLine("********************");
            try
            {
                Console.WriteLine("请输入1~4的数字进行排序");
                Console.WriteLine("1:按攻击力升序排列");
                Console.WriteLine("2:按防御力升序排列");
                Console.WriteLine("3:按血量序排列");
                Console.WriteLine("4:翻转");
                Monster.SortType = int.Parse(Console.ReadLine());

                if (Monster.SortType == 4)
                {
                    monsters.Reverse();
                }
                else
                {
                    monsters.Sort(SortFun);
                }

                //switch(inputIndex)
                //{
                //    case 1:
                //        monsters.Sort((a, b)=>
                //        {
                //            return a.atk > b.atk ? 1 : -1;
                //        });
                //        break;
                //    case 2:
                //        monsters.Sort((a, b) =>
                //        {
                //            return a.def > b.def ? 1 : -1;
                //        });
                //        break;
                //    case 3:
                //        monsters.Sort((a, b) =>
                //        {
                //            return a.hp > b.hp ? 1 : -1;
                //        });
                //        break;
                //    case 4:
                //        //翻转API
                //        monsters.Reverse();
                //        break;
                //}

                for (int i = 0; i < 10; i++)
                {
                    Console.WriteLine(monsters[i]);
                }
            }
            catch
            {
                Console.WriteLine("请输入数字");
            }
        }
    }

2.练习题2

    //写一个物品类(类型,名字,品质),创建10个物品
    //添加到List中
    //同时使用类型、品质、名字长度进行比较
    //排序的权重是:类型>品质>名字长度
    class Item
    {
        public int type;
        public string name;
        public int quality;

        public Item( int type, string name, int quality )
        {
            this.type = type;
            this.name = name;
            this.quality = quality;
        }

        public override string ToString()
        {
            return string.Format("道具信息-类型{0} 名字{1} 品质{2}", type, name, quality);
        }
    }
     class Program
    {
        static void Main(string[] args)
        {
        	        	            List<Item> itemList = new List<Item>();
            Random r = new Random();
            for (int i = 0; i < 10; i++)
            {
                itemList.Add(new Item(r.Next(1, 6), "Item" + r.Next(1, 200), r.Next(1, 6)));
                Console.WriteLine(itemList[i]);
            }

            itemList.Sort((a, b) =>
            {
                //类型不同 按类型比
                if( a.type != b.type )
                {
                    return a.type > b.type ? -1 : 1;
                }
                //按品质比
                else if( a.quality != b.quality )
                {
                    return a.quality > b.quality ? -1 : 1;
                }
                //否则就直接按名字长度比
                else
                {
                    return a.name.Length > b.name.Length ? -1 : 1;
                }
            });

            Console.WriteLine("*********************");
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine(itemList[i]);
            }
        }
     }

3.练习题3


    //linq  SQL

    //请尝试利用List排序方式对Dictionary中的内容排序
    //提示:得到Dictionary的所有键值对信息存入List中
     class Program
    {
        static void Main(string[] args)
        {
			            Dictionary<int, string> dic = new Dictionary<int, string>();
            dic.Add(2, "123123");
            dic.Add(6, "123123");
            dic.Add(1, "123123");
            dic.Add(4, "123123");
            dic.Add(3, "123123");
            dic.Add(5, "123123");

            List<KeyValuePair<int, string>> list = new List<KeyValuePair<int, string>>();

            foreach (KeyValuePair<int, string> item in dic)
            {
                list.Add(item);
                Console.WriteLine(item.Key + "_" + item.Value);
            }

            list.Sort((a, b) =>
            {
                return a.Key > b.Key ? 1 : -1;
            });

            for (int i = 0; i < list.Count; i++)
            {
                Console.WriteLine(list[i].Key + "_" + list[i].Value);
            }
        }

        static int SortFun(Monster m1, Monster m2)
        {
            switch (Monster.SortType)
            {
                case 1:
                    return m1.atk > m2.atk ? 1 : -1;
                case 2:
                    return m1.def > m2.def ? 1 : -1;
                case 3:
                    return m1.hp > m2.hp ? 1 : -1;
            }
            return 0;
        }
        }
     }
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

四月的白羊座

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值