// Sort matrix using vector.
// By DreamVB 18:47 17/10/2016

#include <iostream>
#include <vector>
#include <algorithm>
#include <time.h>

using namespace std;
using std::cout;
using std::endl;

bool compareFunction(int i, int j)
{
	return (i<j);
}

int main(int argc, char *argv[]){
	const int ROWS = 6;
	const int COLS = 6;
	int matrix1[ROWS][COLS] = { 0 };
	vector<int>matrix_sort;

	int i = 0;
	int j = 0;
	int t = 0;

	cout << " UNSORTED MATRIX 1" << endl;

	//Fill first matrix's with random numbers
	for (i = 0; i < ROWS; i++){
		for (j = 0; j < COLS; j++){
			//Write out table value.
			matrix1[i][j] = rand() % 9;
		}
	}

	//Print matrix 1
	for (i = 0; i < ROWS; i++){
		for (j = 0; j < COLS; j++){
			//Write out table value.
			cout << " " << matrix1[i][j];
		}
		cout << endl;
	}

	cout << endl;

	//Add matrix items to a vector.
	for (i = 0; i < ROWS; i++){
		for (j = 0; j < COLS; j++){
			//Write out table value.
			matrix_sort.push_back(matrix1[i][j]);
		}
	}

	//Sort the vector
	std::sort(matrix_sort.begin(), matrix_sort.end(), compareFunction);
	
	//Place sorted items back into matrix.
	for (i = 0; i < ROWS; i++){
		for (j = 0; j < COLS; j++){
			//Write out table value.
			matrix1[i][j] = matrix_sort[i * 6 + j];
		}
	}

	//Print sorted matrix
	cout << " SORTED MATRIX 1" << endl;

	for (i = 0; i < ROWS; i++){
		for (j = 0; j < COLS; j++){
			//Write out table value.
			cout << " " << matrix1[i][j];
		}
		cout << endl;
	}

	matrix_sort.clear();
	//Pause
	system("pause");
	return 0;
}