order 2d array in c++

#include <iostream>
#include <algorithm>

int main() {
    const int rows = 3;
    const int cols = 4;

    int arr[rows][cols] = {
        {5, 2, 8, 12},
        {1, 7, 6, 4},
        {3, 9, 10, 11}
    };

    std::cout << "Original 2D Array:\n";
    for (int i = 0; i < rows; ++i) {
        for (int j = 0; j < cols; ++j) {
            std::cout << arr[i][j] << " ";
        }
        std::cout << "\n";
    }

    // Flatten the 2D array for sorting
    int* flatArr = &arr[0][0];
    int totalElements = rows * cols;

    // Sorting the flattened array
    std::sort(flatArr, flatArr + totalElements);

    // Reconstructing the 2D array from the sorted flat array
    for (int i = 0; i < rows; ++i) {
        for (int j = 0; j < cols; ++j) {
            arr[i][j] = flatArr[i * cols + j];
        }
    }

    std::cout << "\nSorted 2D Array:\n";
    for (int i = 0; i < rows; ++i) {
        for (int j = 0; j < cols; ++j) {
            std::cout << arr[i][j] << " ";
        }
        std::cout << "\n";
    }

    return 0;
}