C# List排序 List.Sort();

委托 lambda表达式 类继承接口

cs 复制代码
class Program
    {
    public static void Main()
    {
        List<Test > tests = new List<Test>();
        tests.Add(new Test (1));
        tests.Add(new Test (4));
        tests.Add(new Test (2));
        tests.Add(new Test (3));
        tests.Add(new Test (5));
        
        //用委托
        tests.Sort( delegate (Test a, Test b) 
        {
            if (a.Id > b.Id)
            {
                return -1;
            }
            else if (a.Id < b.Id)
                { return 1; }
            return 0;
        });
        for (int i = 0; i < tests.Count; i++)
        {
            Console.WriteLine(tests[i].Id);
        }

 Console.WriteLine("-----------------------------------------");
        // 用lambda表达式
        tests.Clear();
        tests.Add(new Test(1));
        tests.Add(new Test(4));
        tests.Add(new Test(2));
        tests.Add(new Test(3));
        tests.Add(new Test(5));

        tests .Sort( (a, b)=>
        { 
            if(a.Id>b.Id)
                return 1;
            return -1;
        });

        //把if  else 写成三目运算符
        tests.Sort((a, b) => { return (a.Id > b.Id) ? 1 : -1; });
       
        for (int i = 0; i < tests.Count; i++) 
            {
                Console.WriteLine(tests[i].Id);
            }

Console.WriteLine("-----------------------------------------");
        //用类继承接口    实现好接口里的逻辑后就可以直接用啦
        tests.Clear();
        tests.Add(new Test(1));
        tests.Add(new Test(4));
        tests.Add(new Test(2));
        tests.Add(new Test(3));
        tests.Add(new Test(5));
        tests.Sort();

        for (int i = 0; i < tests.Count; i++)
        {
            Console.WriteLine(tests[i].Id);
        }
    }

    }
class Test : IComparable<Test>
{
     public int Id { get; set; }
    public Test( int id)
    { 
        Id = id;
    }

    public int CompareTo(Test other)
    {                               // 返回正数(1) 放后面
                                    //返回负数(-1) 放前面
        if(this.Id>other.Id)        //返回0  不动
            return 1;               //如果我比他大 我就放他后面(返回1)
        else if(this.Id<other.Id)   //如果我比他小 我就放他前面(返回-1)
            return -1; 
        
        return 0;                   //如果我们相等 就不动
    }                               //这样一来 就是升序排列了
}

根据不同词条排序

cs 复制代码
class Program
{
    public static void Main()
    {   
        Practice1 practice1 = new Practice1();
        practice1.MonsterSort();
    

    }
    

}
class Practice1
{
    int positive = 1;
    int negative = -1;
    int a;
    List<Monster> list = new List<Monster>();
    public Practice1()
    {
        list.Add(new Monster(1, 3, 6));
        list.Add(new Monster(2, 4, 8));
        list.Add(new Monster(7, 7, 7));
        list.Add(new Monster(5, 6, 3));
        list.Add(new Monster(7, 9, 1));
        list.Add(new Monster(4, 5, 1));
        list.Add(new Monster(3, 3, 5));
        list.Add(new Monster(11, 3, 9));
        list.Add(new Monster(5, 11, 10));
        list.Add(new Monster(6, 10, 12));
    }

    public void MonsterSort()
    {
        while (true)
        {
            Console.WriteLine("");
            Console.WriteLine("请输入数字进行排序");
            Console.WriteLine("1.攻击排序");
            Console.WriteLine("2.防御排序");
            Console.WriteLine("3.血量排序");
            Console.WriteLine("4.反转");
            a = int.Parse(Console.ReadLine());


            if (a == 4)
            {
                positive = -positive;
                negative = -negative;

            }
            else 
            {
                list.Sort(SortFun);
            }
            for (int i = 0; i < list.Count; i++)
            {
                Console.WriteLine(list[i]);
            }
            
        }
    }

    public int SortFun(Monster c ,Monster b)
    {
        switch (a)
        {
            case 1:
                return (c.Atk > b.Atk ? positive  : negative);
            case 2:
                return (c.Def > b.Def ? positive : negative);
            case 3:
                return (c.Def > b.Def ? positive : negative);
        }
        return 0;
    }
   
}

class Monster 
{
     public int Atk { get; set; }
     public int Def { get; set; }
     public int Hp { get; set; }

    public Monster(int atk ,int def ,int hp)
    { 
        Atk = atk; 
        Def = def; 
        Hp = hp;
    }
    public override string ToString()
    {
        return String.Format("怪物血量{0} 攻击力{1} 防御力{2}",Hp,Atk,Def);
    }


}

根据不同词条的权重排序

(类型>名字长度>品质)

cs 复制代码
class Program
{
    public static void Main()
    {   
        Practice1 practice2 = new Practice2();
        practice1.ObjectsSort();

    }

}
namespace Advanced
{
    internal class practice2
    {
        List<Objects> practice=new List<Objects>();
        public practice2()
        {
            practice.Add(new Objects(Types.Weapon, "手枪", Value.C));
            practice.Add(new Objects(Types.Weapon, "散弹枪", Value.B));
            practice.Add(new Objects(Types.Weapon, "神威大炮", Value.A));
            practice.Add(new Objects(Types.Armor, "皮衣", Value.C));
            practice.Add(new Objects(Types.Jewelry, "碎骨项链", Value.F));
            practice.Add(new Objects(Types.Armor, "铁胸甲", Value.B));
            practice.Add(new Objects(Types.Weapon, "小步枪", Value.C));
            practice.Add(new Objects(Types.Jewelry, "麻痹戒指", Value.A));
            practice.Add(new Objects(Types.Armor, "铁鞋子", Value.B));
            practice.Add(new Objects(Types.Jewelry, "雷电手环", Value.A));
            practice.Add(new Objects(Types.Weapon, "大AK47", Value.A));
        }

        public void ObjectsSort()
        {
            practice.Sort((a, b) => {
                if (a.objectType > b.objectType)
                    return -1;
                else if(a.objectType<b.objectType) 
                    return 1;
                else if (a.name.Length>b.name.Length)
                    return -1;
                else if(a.name.Length < b.name.Length)
                    return 1;
                else if(a.value<b.value)
                    return -1;
                else return 1;

            
            });
            ShowList();
        }
        public void ShowList()
        {
            for (int i = 0; i < practice.Count; i++)
            {
                Console.WriteLine("{0}  {1}  {2}", practice[i].objectType, practice[i].name, practice[i].value);
            }
        }
    }
    enum Types
    {
        Weapon,
        Armor,
        Jewelry,
    }
    enum Value
    {
        A, B, C, D, E, F,
    }
    class Objects
    {
       public  Types objectType;
        public  String name;
        public  Value value;
        public Objects(Types t,string n,Value v ) 
        {
          objectType = t;
            name = n;
            value = v;
        }
        
    }
}

把字典键值对存List 里面根据键(或者值进行排序)

cs 复制代码
namespace Advanced
{
    internal class practice3
    {
        Dictionary<int, string> dir = new Dictionary<int, string>();
       List<KeyValuePair<int,string>> pairsList = new List<KeyValuePair<int,string>>();
        public practice3()
        {
            dir.Add(4, "abc");
            dir.Add(1, "bcd");
            dir.Add(2, "cde");
            dir.Add(3, "def");
        }

        public void DirectorySort()
        {
            foreach (KeyValuePair<int ,string> a in dir)
            {
                pairsList.Add(a);
            }

            pairsList.Sort((a, b) => 
            {
                if (a.Key < b.Key)
                {
                    return -1;
                }
                else
                    return 1;
            });

            foreach (KeyValuePair<int, string> a in pairsList)
            {
                Console.WriteLine("{0}-{1}",a.Key,a.Value);
            }
        }
        
    }
}
相关推荐
Clockwiseee6 小时前
php伪协议
windows·安全·web安全·网络安全
向宇it6 小时前
【从零开始入门unity游戏开发之——C#篇25】C#面向对象动态多态——virtual、override 和 base 关键字、抽象类和抽象方法
java·开发语言·unity·c#·游戏引擎
唐宋元明清21887 小时前
.NET 阻止系统睡眠/息屏
windows·电源
向宇it8 小时前
【从零开始入门unity游戏开发之——C#篇24】C#面向对象继承——万物之父(object)、装箱和拆箱、sealed 密封类
java·开发语言·unity·c#·游戏引擎
yylの博客9 小时前
Windows通过git-bash安装zsh
windows·git·bash·zsh
进击的code10 小时前
windows 下使用WLS2 编译aosp Android14并刷机到pixle 5a
windows
坐井观老天13 小时前
在C#中使用资源保存图像和文本和其他数据并在运行时加载
开发语言·c#
染指111013 小时前
50.第二阶段x86游戏实战2-lua获取本地寻路,跨地图寻路和获取当前地图id
c++·windows·lua·游戏安全·反游戏外挂·游戏逆向·luastudio
dntktop14 小时前
Converseen:全能免费批量图像处理专家
windows
pchmi15 小时前
C# OpenCV机器视觉:模板匹配
opencv·c#·机器视觉