.NET从大到小

时间:2012-02-09 14:09:21

标签: c# .net

我想在.NET(c#)中将数组从最小到最大排序而不使用冒号排序而不使用Datatables.Can任何人都可以帮我完成任务吗?

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();
        }
    }
}

2 个答案:

答案 0 :(得分:2)

使用String.Sort并实现IComparer。你不必把这复杂化。 array.Sort(new YourComparer())。读取MSDN数组排序和IComparer。

答案 1 :(得分:2)

-1表示明显的作业。

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

var sorted = array.OrderByDesc(x =&gt; x)。ToArray();

结束。未经测试,因此可能存在语法错误。使用根据您提供的C#语言有效的LINQ。

相关问题