C# 数组Array.Sort()、List.Sort()排序使用方法

【对一维数组】

  • 数据:int[] A = {16, 67, 12, 50, 8, 46, 4};
  • 整体升序:Array.Sort(A);
  • 整体降序:先升序,再反转Array.Reverse(A);
  • 局部升序: Array.Sort(A,2,4);//从索引为2的元素开始的4个元素进行升序
  • 局部降序:Array.Reverse(A,2,3);//对索引为2的元素开始的3个元素进行反转,这是接着Array.Sort执行的

从左到右的图片分别是从整体升序到局部降序的结果

【对交错数组】

  • 有时我们希望根据交错数组(不是二维数组)中某一行中的某个元素来确定不同行之间的排序,这时就要自定义实现比较方法。对于int类型或string类型的数组,C#内部已经帮你实现了比较方法,所以只需要把数组(具体来说是数组的引用)作为参数传递给Array.Sort()就好了。
  • 代码
using System;
using System.Collections.Generic;

namespace Code
{
    class Program
    {

        static void Main(string[] args)
        {
            //随机生成交错数组
            int[][] nums=new int[10][];
            Random ra=new Random();
            for (int i = 0; i < 10; i++)
            {
                nums[i] = new int[2] {ra.Next(30), ra.Next(30)};//每行只有两个元素
            }
            Array.Sort(nums,new CompareMethod());//调用的方法是public static void Sort<T>(T[] array, IComparer<T> comparer);
            // Array.Sort(nums,2,6,new CompareMethod());//对从索引为2开始的6个元素排序
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine(nums[i][0]+" "+nums[i][1]);
            }
            Console.ReadLine();
        }
        public class CompareMethod : IComparer<int[]>  //继承IComparer<T>接口,T为要比较的元素的类型
        {                                             //类中类,也可以放在类外面
            public  int Compare(int[] x, int[] y)
            {
                return x[0] - y[0];//返回值大于0表示x>y,等于0表示x=y,小于0表示x<y。Array.Sort内部会根据这个返回值来判断x和y的大小关系,并把小的元素放在前面
                                   //如果想降序怎么办,返回y[0]-x[0]即可
            }
        }
    }

    
}
  •  输出结果

【结构体数组】

  • 每次继承接口写起来会比较麻烦,更简单的方法是用委托,也即调用public static void Sort<T>(T[] array, Comparison<T> comparison);方法,Comparison<T>是一个泛型委托public delegate int Comparison<in T>(T x, T y);
  • 对学生的成绩进行排序,希望成绩高的排在前面,对成绩相同的学号小的排在前面
  • 代码
using System;
using System.Collections.Generic;

namespace 笔试
{
    class Program
    {

        static void Main(string[] args)
        {
            Student[] nums = new Student[5];
            nums[0]=new Student(1001,90,"张");
            nums[1]=new Student(1009,83,"王");
            nums[2]=new Student(1004,88,"李");
            nums[3]=new Student(1002,88,"何");
            nums[4]=new Student(1005,93,"赵");
            Array.Sort(nums,CompareMethod);
            for (int i = 0; i < nums.Length; i++)
            {
                Console.WriteLine("第"+(i+1)+"名"+nums[i].name+" 学号:"+nums[i].id+" 分数:"+nums[i].score);
            }
            Console.ReadKey();
        }

        public struct Student
        {
           public int id;
           public int score;
           public string name;

            public Student(int id, int score, string name)
            {
                this.id = id;
                this.score = score;
                this.name = name;
            }
        }

        public static int CompareMethod(Student a, Student b)
        {
            int temp = b.score - a.score;
            if (temp == 0)
            {
                temp = a.id - b.id;
            }
            return temp;
        }
    }  
}
  • 输出结果

 

【使用Lambda表达式】

  • lambda表达式可以基于很简单的方法生成委托,且避免了需要声明新方法(及CompareMethod)的麻烦,使得代码更简单。当然,另一方面不熟悉的话也更不好理解。
  • 只需要对调用的Array.Sort简单改一下即可
  Array.Sort(nums, (a, b) =>
            {
                int temp = b.score - a.score;
                if (temp == 0)
                    temp = a.id - b.id;
                return temp;
            });
            //注解:
            //a b就是参数的名字,相当之前的Student a,Student b,只不过把Student省略了,默认了是Student类型的,所以参数名字任意,写成aaa,bbb都可以
            //参数要用括号括起来,就跟一般方法的参数要用括号一样
            //之后跟着“=>”号
            //接下来是花括号,就跟一般方法要加花括号一样
            //在花括号内部的代码和之前的CompareMethod方法完全一样
            //关于返回值和参数,要看这个委托定义的是什么样子的

【List.Sort()排序】

List.Sort方法内部调用了Array.Sort,排序实现完全和Array.Sort一样

  • 18
    点赞
  • 71
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值