-4
public void ascendingOrder()
{
    // helper class
    double temp = 0;

    for (int j = 0; j < numbers.Length; j++)
    {
        for (int i = 0; i < numbers.Length - 1; i++)
        {
            if (numbers[i] > numbers[i + 1])
            {
                temp = numbers[i];
                numbers[i] = numbers[i + 1];
                numbers[i + 1] = temp;
            }
        }
    }
}

i need the code to sort the columns first and then sort the rows like the following example!

input :

n=3

2 2 1 
3 53 4 
32 5 3 

output:

1 2 2 
3 3 54 
4 32 53 
6
  • 3
    there's some inconsistency with your output Commented Apr 24, 2014 at 11:45
  • 1
    input: n=3? And what is that suppose to mean? Commented Apr 24, 2014 at 11:45
  • Why you just not use Array.Sort and then if you want Reverse?! Commented Apr 24, 2014 at 11:47
  • 2
    Your program operates on one dimensional numbers array. Are we supposed to extend you that program for two dimensional arrays? That is not how Stack Overflow works... Commented Apr 24, 2014 at 11:47
  • 2
    first correct the output, numbers are different from what we see in input. Commented Apr 24, 2014 at 11:48

3 Answers 3

1

Some helpful links:

Sign up to request clarification or add additional context in comments.

Comments

1
int[] array = new int[] { 3, 1, 4, 5, 2 };
Array.Sort<int>(array,
                new Comparison<int>(
                        (i1, i2) => i1.CompareTo(i2)
                ));

Better way to sort array in descending order

Comments

0

I believe this person is currently writing an exam, so linq methods will not make him pass this test. Try with this code.

        double[,] numbers = new double[,]{ { 1, 3, 2 }, { 4, 6, 5 }, { 7, 9, 8 } };
        // helper class
        double temp = 0;

        for (int k = 0; k < 3; k++)
        {
            for (int j = 0; j < 2; j++)
            {
                for (int i = 1; i < 3; i++)
                {
                    if (numbers[k,i] < numbers[k,j])
                    {
                        temp = numbers[k,i];
                        numbers[k,i] = numbers[k,j];
                        numbers[k,j] =temp;
                    }
                }
            }
        }

2 Comments

And how rewriting an answer will help him to learn something? Aren't exams for such purpose?
Let him learn the hard way once he finds a job.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.