0

Sorting a 2-dimensional can be done in two ways :

  1. Converting 2-dimensional to 1-dimensional and then sorting it.
  2. Directly sorting the 2-dimensional array.

I would like to know the second method. Sorting it directly.

2
  • 3
    Google is your friend Commented Mar 27, 2015 at 4:04
  • Formatting the text to make it more readable Commented Mar 27, 2015 at 4:50

1 Answer 1

1

Try this. You can sort two dimensional array Directly

        int array[][] = { { 12, 43, 21, 87, 32 }, { 43, 75, 21, 45, 65 } };

    int t = 0;
    for (int x = 0; x < 2; x++) {
        for (int y = 0; y < 5; y++) {
            for (int i = 0; i < 2; i++) {
                for (int j = 0; j < 5; j++) {
                    if (array[i][j] > array[x][y]) {
                        t = array[x][y];
                        array[x][y] = array[i][j];
                        array[i][j] = t;
                    }
                }
            }
        }
    }

    System.out.println("The Sorted Array:");
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 5; j++) {
            System.out.print(array[i][j] + "\t");
        }
        System.out.println();
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.