Click here to Skip to main content
15,888,313 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.

C#
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]);
                }
                }
        }
    }
}
Posted
Updated 11-Aug-14 21:02pm
v2
Comments
Dilan Shaminda 12-Aug-14 2:45am    
You are asking a solution without saying what is the problem with your code.
CPallini 12-Aug-14 2:49am    
The problem is you aren't sorting it at all. What do you want to sort? I mean are you trying to sort the array on second dimension?
Sergey Alexandrovich Kryukov 12-Aug-14 2:53am    
No, if you want to get help, please you tell us what is the problem. Start with explanation of what do you want to achieve, it's not obvious. What should be the result of sorting?
—SA

1 solution

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).

Fix the above and one might help. I guess when you have described your sorting (and fixed the other broken code), you gave the answer youreslf. ;-)
Cheers
Andi
 
Share this answer
 
Comments
Andreas Gieriet 12-Aug-14 6:15am    
Any comment from those who down vote? I'd like to discuss my/your point of view.
Cheers
Andi

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900