-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 
Konrad Kokosa
  • 16,563
  • 2
  • 36
  • 58
user3568592
  • 7
  • 1
  • 3

3 Answers3

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

Community
  • 1
  • 1
Bassam Alugili
  • 16,345
  • 7
  • 52
  • 70
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;
                    }
                }
            }
        }
leskovar
  • 661
  • 3
  • 8