Schow javascript associative array in html table using HTML : I need to print out the results of a sort on an associative array in javascript in a HTML table not using PHP or those things.
The sorting works fine as I can see that in the console but it doesn't show the table. Here is the source as far as I have it now:

    <!DOCTYPE html>
    <html>
    <body>

    <button onclick="sortarray('name')">Sort by Name</button>

    <button onclick="sortarray('year')">Sort by Year</button>





    <p id="demo">test</p>
 
    <script>



    var array = [   
	 {
		name: 'TK345',
		year: 2011,
		custom: 456,
		colour: 'red'
	 },
	 {

		name: 'ZJ456',
		year: 2001,
		custom: 96,
		colour: 'black'
	},
	{            
		name: 'AW364',
		year: 1985,
		custom: 001,
		colour: 'cyan'
	},    
	{
		name: 'RT112',
		year: 2012,
		custom: 33,
		colour: 'green'
	},
	{
		name: 'PO445',
		year: 2012,
		custom: 11,
		colour: 'yellow'
	   }
    ];

    function sortarray(sorter) {

    if (array.length < 1) {
        return -1;
     }

    if(sorter == "name"){

	var byName = array.slice(0);
	var tableout = document.createElement('table');
	byName.sort(function(a,b) {
	
	var x = a.name.toLowerCase();
	var y = b.name.toLowerCase();
	return x < y ? -1 : x > y ? 1 : 0;
	
		
		tableout.setAttribute('border','1');
		tableout.setAttribute('width','100%')
		var row = tableout.insertRow(0);
		for(j=1; j<=5; j++){
		row = tableout.insertRow(j-1);
		for (i=1; i<=4; i++)
		var text = document.createTextNode(byName);
		var cell = row.insertCell(i-1);
		cell.setAttribute('align','center')
		cell.appendChild(text);
		}
		})
	document.getElementById("demo").appendChild(tableout);
	console.log(byName);

	}
  

    else if(sorter == "year"){

	var byDate = array.slice(0);
	var tableout = document.createElement('table');
	byDate.sort(function(a,b) {
	
	return a.year - b.year;
	
		tableout.setAttribute('border','1');
		tableout.setAttribute('width','100%')
		var row = tableout.insertRow(0);
		for(j=1; j<=5; j++){
		row = tableout.insertRow(j-1);
		for (i=1; i<=4; i++)
		var text = document.createTextNode(byDate);
		var cell = row.insertCell(i-1);
		cell.setAttribute('align','center')
		cell.appendChild(text);
		}
		})
	document.getElementById("demo").appendChild(tableout);
	console.log(byDate);

	}

    else {};
	
    };


    </script>

    </body>
    </html>

So it doesn't show me anything of that table on the page or in the console. 

Any ideas?

Thanks in advance.

TheVagabond