I found a similar post JavaScript - Sort an array based on another array of integers and just need some help implementing that script with my own script.
I'm working on a fiddle http://jsfiddle.net/c87Jx/ which displays a table based on the array array
's data.
My script:
//<![CDATA[
//employee array
window.onload = function () {
var array = [];
array[0] = {
name: "John Doe",
number: "508-555-1234"
};
array[1] = {
name: "Jane Doe",
number: "775-555-5678"
};
array[2] = {
name: "Adam Doe",
number: "603-555-9012"
};
array[3] = {
name: "Karen Doe",
number: "978-555-3456"
};
array[4] = {
name: "Trish Doe",
number: "603-555-7890"
};
array[5] = {
name: "Company",
number: "(try to reach someone)"
};
var arrayLength = array.length;
var theTable = document.createElement("table");
currentIndex = 0;
lastIndex = array.length - 1;
//write table header
while (true) {
tr = document.createElement("tr");
td0 = document.createElement("td");
td0.colSpan = 3;
p = document.createElement("p");
p.appendChild(document.createTextNode("Table"));
td0.appendChild(p);
tr.appendChild(td0);
theTable.appendChild(tr);
break;
}
//for each employee in above array, write the attempt #, their name, and their contact info
for (var i = 0, tr, td; i < arrayLength; i++) {
tr = document.createElement("tr");
td1 = document.createElement("td");
p = document.createElement("p");
p.appendChild(document.createTextNode("Attempt #" + (array.length++-5)));
td1.appendChild(p);
tr.appendChild(td1);
td2 = document.createElement("td");
p = document.createElement("p");
p.appendChild(document.createTextNode(array[i].name));
td2.appendChild(p);
tr.appendChild(td2);
td3 = document.createElement("td");
p = document.createElement("p");
p.appendChild(document.createTextNode(array[i].number));
td3.appendChild(p);
tr.appendChild(td3);
theTable.appendChild(tr);
}
document.getElementById("table").appendChild(theTable);
};
//]]>
Outputs a pretty basic table like:
<div id="table">
<table>
<tr>
<td colspan="3"><p>Table</p></td>
</tr>
<tr>
<td><p>Attempt #1</p></td>
<td><p>John Doe</p></td>
<td><p>508-555-1234</p></td>
</tr>
<tr>
<td><p>Attempt #2</p></td>
<td><p>Jane Doe</p></td>
<td><p>775-555-5678</p></td>
</tr>
<tr>
<td><p>Attempt #3</p></td>
<td><p>Adam Doe</p></td>
<td><p>603-555-9012</p></td>
</tr>
<tr>
<td><p>Attempt #4</p></td>
<td><p>Karen Doe</p></td>
<td><p>978-555-3456</p></td>
</tr>
<tr>
<td><p>Attempt #5</p></td>
<td><p>Trish Doe</p></td>
<td><p>603-555-7890</p></td>
</tr>
<tr>
<td><p>Attempt #6</p></td>
<td><p>Company</p></td>
<td><p>(try to reach someone)</p></td>
</tr>
</table>
</div>
Next I want to introduce a second array, which will be used to manage the order in which the data from the first array displays in the outputted table. The goal here is to be able to manipulate the Attempt #s without having to modify the first array.
Example: The first array is written as:
1 - John
2 - Jane
3 - Adam
4 - Karen
5 - Trish
6 - Company
So I want the second array to control the first array's order, so if in the second array I wrote:
var sortArr = [0,3,4,2,5,1];
It would make the first array output like this:
1 - John
2 - Karen
3 - Trish
4 - Adam
5 - Company
6 - Jane
As mentioned above, I found a similar post JavaScript - Sort an array based on another array of integers, and it seems to do exactly what I need. So my question is how do I integrate Nick's script with my current script?