在 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

更新於: 22-6 月-2020

907 次瀏覽

開啟你的 職業生涯

完成課程即可獲得認證

開始
廣告
© . All rights reserved.