如何在 C# 中按升序對陣列進行排序?
首先,設定未排序的陣列。
int[] list = {98, 23, 97, 36, 77};使用 Sort() 方法對陣列進行排序。
Array.Sort(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 + " ");
}
Array.Sort(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 23 36 77 97 98
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP