在 C# 中按字典顺序对单词进行排序
首先,设置一个字符串数组-
string[] arr = new string[] {
"Indian",
"Moroccon",
"American",
};按字典顺序对单词进行排序-
var sort = from a in arr orderby a select a;
示例
让我们看看完整的代码-
using System;
using System.Linq;
class Program {
static void Main() {
string[] arr = new string[] {
"Indian",
"Moroccon",
"American",
};
var sort = from a in arr
orderby a
select a;
foreach(string res in sort) {
Console.WriteLine(res);
}
}
}输出
American Indian Moroccon