using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
// 创建一个包含多个字符串的列表
List<string> fruits = new List<string> { "apple", "orange", "banana", "grape" };
// 使用OrderBy方法按字母顺序对列表进行排序
var sortedFruits = fruits.OrderBy(fruit => fruit);
// 输出排序后的结果
Console.WriteLine("Sorted fruits:");
foreach (var fruit in sortedFruits)
{
Console.WriteLine(fruit);
}
}
}
List<string>: 创建一个字符串类型的列表。OrderBy(fruit => fruit): 使用 LINQ 的 OrderBy 方法对列表中的元素进行排序。这里我们传入一个 lambda 表达式 fruit => fruit,表示按照每个字符串本身的值进行排序(即按字母顺序)。foreach (var fruit in sortedFruits): 遍历排序后的结果并输出每个元素。这段代码展示了如何使用 C# 中的 OrderBy 方法对 List<T> 进行排序。
上一篇:c# ushort
下一篇:c# byte[]转string
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站