I have an array under bahrein
that I would like ordered by the index of each item. My bare code is as shown below.
var bahrein = [
{id: 1, name: "Josef"},
{id: 3, name: "Billy"},
{id: 0, name: "Jane"},
{id: 2, name: "Mack"}
];
for (i = 0; i < bahrein.length; i++){
document.getElementById("show").innerHTML += "<p>"+bahrein[i].name+"</p>";
}
<div id="show"></div>
I have ids assigned for each item, but I placed them out of order. What I would like to know is how to programatically use the sort()
function to list the names on my list in order.
Right now my innerHTML shows the list in the order they are written (ie: Josef
, Billy
, Jane
, and Mack
). I want to show them in the order of their ID (ie: Jane
, Josef
, Mack
, Billy
).