C#中list和数组对元素进行排序

直接看代码,都是用lamda表达式写的,比较简洁,其他方法也可以,不多赘述。
数组降序:

 Array.Sort(arr, (a, b) =>
            {
                int temp = b.id - a.id;
                if (temp > 0)
                {
                    return temp;
                }
                else {
                    return -1;
                }
            });

list降序:

list.Sort((a,b) => {
                int temp = b.id - a.id;
                if (temp > 0)
                {
                    return temp;
                }
                else
                {
                    return -1;
                }
            });

如果想要升序,换下lamda表达式里的返回值就可以了

这是测试工具类方法:

class Cell {
    public int id;
        
    public String name;


    public Cell(int v1, string v2)
    {
        this.id = v1;
        this.name = v2;
    }
}

看排序方法:

private void button1_Click(object sender, EventArgs e)
        {
            Cell[] arr = new Cell[5];
            Cell cell1 = new Cell(2, "222");
            Cell cell2 = new Cell(4, "44444");
            Cell cell3 = new Cell(1, "1111111111111");
            Cell cell4 = new Cell(3, "3333333333");
            Cell cell5 = new Cell(5, "55555555555");
            arr[0] = cell1;
            arr[1] = cell2;
            arr[2] = cell3;
            arr[3] = cell4;
            arr[4] = cell5;
            
            for (int i = 0;i < arr.Length;i++) {
                Console.WriteLine(arr[i].name);

            }
            Console.WriteLine("-------------排序------------------");
            Array.Sort(arr, (a, b) =>
            {
                int temp = b.id - a.id;
                if (temp > 0)
                {
                    return temp;
                }
                else {
                    return -1;
                }

            });
            for (int i = 0; i < arr.Length; i++)
            {
                Console.WriteLine(arr[i].name);

            }
            Console.WriteLine("-------------------------------");
            List<Cell> list = new List<Cell>();
            list.Add(cell1);
            list.Add(cell2);
            list.Add(cell3);
            list.Add(cell4);
            list.Add(cell5);
            for (int i = 0; i < list.Count; i++)
            {
                Console.WriteLine(list[i].name);

            }
            // 降序
            list.Sort((a,b) => {
                int temp = b.id - a.id;
                if (temp > 0)
                {
                    return temp;
                }
                else
                {
                    return -1;
                }

            });
            Console.WriteLine("--------------->--------------");
            for (int i = 0; i < list.Count; i++)
            {
                Console.WriteLine(list[i].name);

            }
        }

看下结果:
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值