C# 排序的多种实现方式

排序是我们编程时的常用操作,实现方式也有很多种,本篇文章列举几种我常用的用法,希望对大家有用!

01

数组排序

最常见的排序是对一个数组排序,比如:

复制代码
  int[] aArray = new int[8] { 18, 17, 21, 23, 11, 31, 27, 38 };

第一种:我们最熟悉的冒泡排序法:即每个值都和它后面的数值比较,每次拿出最小值

复制代码
static void Main(string[] args)        {            int[] aArray = new int[8] { 18, 17, 21, 23, 11, 31, 27, 38 };            for (int j = 0; j < aArray.Length - 1; j++)            {                for (int i = 0; i < aArray.Length - 1; i++)                {                    if (aArray[i] > aArray[i + 1])                    {                        int temp = aArray[i];                        aArray[i] = aArray[i + 1];                        aArray[i + 1] = temp;                    }
                }            }

            foreach (var a in aArray)            {                Console.Write($"{a} ");            }            Console.ReadKey();        }

运行结果:

第二种:利用Array.Sort排序:

① 升序排列:

复制代码
 static void Main(string[] args)        {            int[] aArray = new int[8] { 18, 17, 21, 23, 11, 31, 27, 38 };            Array.Sort(aArray);
            foreach (var a in aArray)            {                Console.Write($"{a} ");            }            Console.ReadKey();        }

运行结果:

② 降序排列:先升序排列,然后对数组反转

复制代码
  static void Main(string[] args)        {            int[] aArray = new int[8] { 18, 17, 21, 23, 11, 31, 27, 38 };            Array.Sort(aArray);            Array.Reverse(aArray);
            foreach (var a in aArray)            {                Console.Write($"{a} ");            }            Console.ReadKey();        }

运行结果:

02

List排序

大多数时候,我们需要将list集合里面的数据进行排序,

① 如果list直接放置的数值类型的数据就比较简单,比如:

复制代码
List<int> aList = new List<int> { 18, 17, 21, 23, 11, 31, 27, 38 };

方法一:利用List<T> 自带的Sort排序方法

-. 升序排列:

复制代码
 aList.Sort();

降序排列:

复制代码
            aList.Sort();            aList.Reverse();

-. sort也可以用如下方式操作:

复制代码
list.Sort((a, b) => a.CompareTo(b));//升序list.Sort((a, b) => b.CompareTo(a));//降序

方法二:利用List的OrderBy与OrderByDescending方法

升序排列:​​​​​​​

复制代码
  List<int> aList = new List<int> { 18, 17, 21, 23, 11, 31, 27, 38 };            aList = aList.OrderBy(a => a).ToList();

降序排列:​​​​​​​

复制代码
  List<int> aList = new List<int> { 18, 17, 21, 23, 11, 31, 27, 38 };            aList = aList.OrderByDescending(a => a).ToList();

方法三:利用link,这种感觉和方法二是一回事

复制代码
 List<int> aList = new List<int> { 18, 17, 21, 23, 11, 31, 27, 38 }; aList = (from a in aList orderby a ascending select a).ToList();//升序 aList = (from a in aList orderby a descending select a).ToList();//降序

②如果list存放的是一个类型,比如:

方法一: 一个集合存放学生的信息,按照学生的成绩升序排列:这里列举了2种方法,方法一注释了

复制代码
 static void Main(string[] args)        {            List<Student> stuList = new List<Student>            {                new Student() {name = "zyr", age = 23, score = 99},                new Student() {name = "zls", age = 25, score = 95},                new Student() {name = "zsq", age = 27, score = 100},                new Student() {name = "zlw", age = 15, score = 69},                new Student() {name = "ywe", age = 17, score = 72},                new Student() {name = "asw", age = 29, score = 58}            };
            //方法1 升序            //stuList.Sort((x, y) => x.score.CompareTo(y.score));            //方法2 升序            stuList = stuList.OrderBy(stu=>stu.score).ToList();
            foreach (var stu in stuList)            {                Console.WriteLine($"{stu}");            }            Console.ReadKey();        }
        public class Student        {            public string name { get; set; }            public int age { get; set; }            public int score { get; set; }
            public override string ToString()            {                return $"姓名是:{name},年龄是:{age},得分是:{score}";            }        } 

方法二: 加入这个学生成绩有重复的,对于重复的成绩按照年龄再排序:这里列举了2种方法,方法一注释了​​​​​​​

复制代码
 static void Main(string[] args)        {            List<Student> stuList = new List<Student>            {                new Student() {name = "zyr", age = 23, score = 99},                new Student() {name = "zls", age = 25, score = 95},                new Student() {name = "zls", age = 22, score = 95},                new Student() {name = "zsq", age = 27, score = 100},                new Student() {name = "zlw", age = 15, score = 69},                new Student() {name = "ywe", age = 17, score = 72},                new Student() {name = "asw", age = 29, score = 58},                new Student() {name = "ywe", age = 18, score = 72},                new Student() {name = "zsq", age = 16, score = 100},            };
            //方法1 升序            //stuList.Sort((x, y) =>            //{            //    int ret = x.score.CompareTo(y.score);            //    if (ret == 0)            //    {            //        return x.age.CompareTo(y.age);            //    }            //    else            //    {            //        return ret;            //    }            //});            //方法2 升序            //stuList = stuList.OrderBy(stu=>stu.score).ThenBy(stu => stu.age).ToList();
            foreach (var stu in stuList)            {                Console.WriteLine($"{stu}");            }            Console.ReadKey();        }
        public class Student        {            public string name { get; set; }            public int age { get; set; }            public int score { get; set; }
            public override string ToString()            {                return $"姓名是:{name},年龄是:{age},得分是:{score}";            }        }
相关推荐
Tony Bai8 分钟前
【Go 网络编程全解】12 本地高速公路:Unix 域套接字与网络设备信息
开发语言·网络·后端·golang·unix
oioihoii18 分钟前
深入理解 C++ 现代类型推导:从 auto 到 decltype 与完美转发
java·开发语言·c++
报错小能手22 分钟前
项目——基于C/S架构的预约系统平台 (1)
开发语言·c++·笔记·学习·架构
MYX_30943 分钟前
第四章 多层感知机
开发语言·python
彬彬醤1 小时前
如何正确选择住宅IP?解析适配跨境、流媒体的网络工具
服务器·开发语言·网络·网络协议·tcp/ip
Yeats_Liao1 小时前
Go Web 编程快速入门 06 - 响应 ResponseWriter:状态码与头部
开发语言·后端·golang
chao1898441 小时前
C#模拟鼠标键盘操作的多种实现方案
开发语言·c#·计算机外设
mit6.8241 小时前
[Agent可视化] 编排工作流(Go) | Temporal引擎 | DAG调度器 | ReAct模式实现
开发语言·后端·golang
Devil枫2 小时前
HarmonyOS鸿蒙应用:仓颉语言与JavaScript核心差异深度解析
开发语言·javascript·ecmascript
future_studio2 小时前
聊聊 Unity(小白专享、C# 小程序 之 联机对战)
unity·小程序·c#