C#中的二维数组排序问题 [英] 2D Array Sorting Problem In C#

查看:71
本文介绍了C#中的二维数组排序问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI!



任何人可以给我最好的解决方案。我想在C#中对2D数组进行排序。



例如



如果我在输入部分输入这个数字。

11,22,1,2,3,4,5,6,7,8,



该计划应该给我这个输出。



1,2,3,4,5 ,, 6,7, 8,11,12



但在程序中没有这样做请解释我的错误是什么。









请告诉我它有什么问题。



HI!

Anyone There Who Can Give Me Best Solution of this. i want to Sort 2D Array IN C#.

For Example

If i enter this number in the input Section .
11,22,1,2,3,4,5,6,7,8,

The Program Should Give Me This Output.

1,2,3,4,5,,6,7,8,11,12

But in it program did not do this please Explain Me What is The Mistake In It.




Please Tell me What Is The Problem In It.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Practice_Csharp_2
{
    enum array:int
    {
        size=5,
        size_1 =2,
    }
    class Program
    {
        static void Main(string[] args)
        {
            int x, y;
            //Declare Array Of Integer
            int[,] a_1=new int[(int)array.size,(int)array.size_1];
            Console.WriteLine("\n\t\tThis is a program for All Basic Array Uses.");
            for (x = 0; x < (int)array.size; x++)
            {
                for (y = 0; y < (int)array.size_1; y++)
                {
                    Console.WriteLine("\nEnter Value In Array [{0}] [{1}] Index.", x, y+1);
                    a_1[x,y] = Convert.ToInt32(Console.ReadLine());
                }
                //Calling Function For Sorting
            }
            sort(a_1);
            Console.ReadLine();

        }
        static void sort(int[,] a_1)
        {
            int x, y;
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine("\n\t\tAfter Sorting Of Array.");
            Console.ResetColor();
            for (x = 0; x < (int)array.size; x++)
            {
                for (y = 0; y < (int)array.size_1-1; y++)
                {
                    if (a_1[x, y] > a_1[x, y + 1])
                    {
                        int temp;
                        temp = a_1[x, y];
                        a_1[x, y] = a_1[x, y + 1];
                        a_1[x, y + 1] = temp;

                    }
                }
            }
            for (x = 0; x < (int)array.size; x++)
            {
                for (y = 0; y < (int)array.size_1; y++)
                {
                    Console.WriteLine(a_1[x,y]);
                }
                }
        }
    }
}

推荐答案

查看细节,我可以说你的代码闻起来。



  • 命名根本没有帮助,例如命名枚举数组充其量只是误导
  • 强制转换是一种嗅觉:为什么将常量定义为枚举而不是普通的int?
  • 你的循环条件被破坏了对于size_1:为什么在检查less-than时从边界中减去1?
  • 目前还不清楚预期的排序是什么:实现的情况对我没有任何意义。在排序函数之上添加注释,准确描述排序在此Universe中的含义。
  • 为什么在循环中有一个注释告诉您调用排序在这里,但排序被称为外部循环。不要试图欺骗任何人(包括你自己)。
Without looking into the details I can say that your code "smells".

  • naming is not helping at all, e.g. naming an enum "array" is misleading at best
  • casts are a smell at first sight: why do you define constants as enum instead of a plain int?
  • your loop conditions are broken for size_1: why do you subtract 1 from the boundary while checking for less-than?
  • it is unclear what the intended sorting is: the implemented case does not mean anything to me. Add a comment on top of the sort function describing exactly what the "sorting" means in this universe.
  • Why do you have a comment within the loop telling that you call the sorting here, but the sorting is called outside the loop. Don't try to fool anyone (including yourself).


这篇关于C#中的二维数组排序问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆