.NET从最大到最小

本文关键字:NET | 更新日期: 2023-09-27 18:24:06

我想在.NET(c#)中从最小到最大对数组进行排序,而不使用Bubble sort和Datatables。有人能帮助我如何完成任务吗?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //int[,] myArray = new int[4, 2];

            //int[,] array_sorted = { { 20, 9, }, { 75, 25 }, { 90, 78, } };
            int[,] array_sorted = { { 20, 9, }, { 75, 25 }, { 50, 92 }, { 9, 7 }, { 19, 78 }, { 50, 78 }, { 50, 98 }, { 23, 32 }, { 12, 232 }, { 45, 65 } };

            Console.WriteLine("Before Bubble Sorting....");
            for (int i = 0; i < array_sorted.GetLength(0); i++)
            {
                for (int j = 0; j < array_sorted.GetLength(1); j++)
                {
                    Console.Write("{0,3}", array_sorted[i, j]); // respresent how the element should be represented
                }
                Console.WriteLine();
            }
            Console.WriteLine("After Bubble Sorting...");
            for (int i = 0; i < array_sorted.GetLength(0); i++) // Array Sorting
            {
                for (int j = array_sorted.GetLength(1) - 1; j > 0; j--)
                {
                    for (int k = 0; k < j; k++)
                    {
                        if (array_sorted[i, k] > array_sorted[i, k + 1])
                        {
                            int temp = array_sorted[i, k];
                            array_sorted[i, k] = array_sorted[i, k + 1];
                            array_sorted[i, k + 1] = temp;
                        }
                    }
                }
            }
            for (int i = 0; i < array_sorted.GetLength(0); i++)
            {
                for (int j = 0; j < array_sorted.GetLength(1); j++)
                {
                    Console.Write("{0 ,3}", array_sorted[i, j]);
                }
                Console.WriteLine();
            }



            Console.ReadLine();
        }
    }
}

.NET从最大到最小

使用字符串。排序并实现IComparer。你不必这么复杂。大堆排序(new YourCompany())。阅读MSDN数组排序和IComparer。

-1用于明显的家庭作业。

无论如何,这里有一个简单的答案:

var sorted=数组。OrderByDesc(x=>x).ToArray();

完成。未测试,因此可能存在语法错误。使用LINQ,根据您作为语言给出的C#,它是有效的。

相关文章:
  • 没有找到相关文章