Ketan Arwat
Ketan Arwat

Reputation: 3

2-dimensional Array Sorting

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.

Upvotes: 0

Views: 138

Answers (1)

Rishi Paul
Rishi Paul

Reputation: 936

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();
    }

Upvotes: 1

Related Questions