如何使用 Array 類的**法**對一維陣列按降序排序?


以下是一個未排序的陣列。

int[] list = {98, 23, 97, 36, 77};

現在首先使用 Sort() 方法對陣列進行排序。

Array.Reverse(list);

使用 Reverse() 方法,該方法最終會生成一個按降序排列的已排序陣列。

Array.Reverse(list);

您可以嘗試執行以下程式碼,以按降序對一維陣列進行排序。

示例

 線上演示

using System;
namespace Demo {
   public class MyApplication {
      public static void Main(string[] args) {
         int[] list = {98, 23, 97, 36, 77};
         Console.WriteLine("Original Unsorted List");
         foreach (int i in list) {
            Console.Write(i + " ");
         }
         //sort
         Array.Sort(list);
         // Descending order
         Array.Reverse(list);
         Console.WriteLine("
Sorted List");          for(int i=0; i<list.Length; i++) {             Console.Write(list[i] + " ");          }       }    } }

輸出

Original Unsorted List
98 23 97 36 77
Sorted List
98 97 77 36 23

更新於: 2020-6-23

211 次瀏覽

開啟你的 職業生涯

完成課程認證

開始學習
廣告
© . All rights reserved.