using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
List<int> numbers = new List<int> { 5, 2, 9, 1, 5, 6 };
// 使用 Sort 方法对列表进行排序
numbers.Sort();
// 输出排序后的列表
Console.WriteLine("排序后的列表:");
foreach (int number in numbers)
{
Console.WriteLine(number);
}
}
}
List<int>
:创建一个整数类型的列表,并初始化一些数据。numbers.Sort()
:调用 Sort
方法对列表中的元素进行升序排序。foreach
循环:遍历排序后的列表并输出每个元素。如果你需要对自定义对象进行排序,可以实现 IComparable
接口或使用 Comparison<T>
委托来定义排序逻辑。
上一篇:c# 注释
下一篇:c# byte[]
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站