如何在 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

更新于: 2020 年 6 月 23 日

9K+ 浏览量

开启您的 职业生涯

通过完成课程获得认证

开始
广告
© . All rights reserved.