对整数数组进行排序

Luc*_*uca -3 c# arrays

我的作业有问题。我必须对数组中的数字进行排序,从最小到最大。这是代码: 源代码

在这个例子中我确实必须使用一个函数,但我不知道如何消除这个错误。我希望你可以帮助我!

小智 6

如果您不必使用函数来执行此操作,您可以使用如下所示的内容:

using System;
namespace Stackoverflow
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] myArr = { 5, 17, 23, 9, 8, 10 };
            Array.Sort(myArr);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如果你必须使用一个函数,我会使用这样的东西:

using System;
namespace Stackoverflow
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] myArr = { 5, 17, 23, 9, 8, 10 };
            ReturnSorted(myArr);
        }

        public static int[] ReturnSorted(int[] secondArr)
        {
            int[] sorted = new int[secondArr.Length];
            for (int i = 0; i < secondArr.Length; i++)
            {
                sorted[i] = secondArr[i];
            }
            Array.Sort(sorted);
            return sorted;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

您可以使用以下命令清空函数中的数组“secondArr”:“ Array.Clear(secondArr, 0, secondArr.Length);”以节省内存。

我希望我能帮忙!:D