C#的排序方法 OrderBy
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Runtime.ExceptionServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
// 命名空间
namespace pro01
{
// 类
internal class Program
{
// main 方法
static void Main(string[] args)
{
// LINQ 常用的扩展方法 IEnumerable<T> 扩展方法
// 数组 List 集合,Dictionary 字典,都是实现 IEnumerable 接口的扩展方法
// 初始化数据
List<Employee> list = new List<Employee>(); // 新建类型为 Employee 的集合数据
list.Add(new Employee {Id = 1,Name = "AAA",Age = 10,Gender = true ,Salary = 1000 });
list.Add(new Employee {Id = 2,Name = "BBB",Age = 11,Gender = false ,Salary = 2000 });
list.Add(new Employee {Id = 2,Name = "BBB",Age = 11,Gender = false ,Salary = 2000 });
list.Add(new Employee {Id = 2,Name = "BBB",Age = 11,Gender = false ,Salary = 2000 });
list.Add(new Employee {Id = 1,Name = "BBB",Age = 11,Gender = false ,Salary = 2000 });
list.Add(new Employee {Id = 34,Name = "BBB",Age = 11,Gender = false ,Salary = 2000 });
list.Add(new Employee { Id = 3,Name = "CCC",Age = 121,Gender = true ,Salary = 7000 });
list.Add(new Employee { Id = 3,Name = "CCC",Age = 1,Gender = true ,Salary = 3000 });
Console.WriteLine("list 数据");
foreach (Employee i in list)
{
Console.WriteLine(i);
}
// TODO 排序
// Order 正序
IEnumerable<Employee> e1 = list.OrderBy(row => row.Age).ThenBy(item => item.Id);
Console.WriteLine("正序排序按照 从小到大 Age 然后按照 ID 排序");
foreach (Employee i in e1)
{
Console.WriteLine(i);
}
// OrderDescending 倒叙排序 【由大到小】
Console.ReadLine();
}
}
}