?I want to sort it by lastname not firstname. The list should be
displayed firstname followed by lastname.
fullnamearray.sort( function(a,b){
var lastNameA = a.split(" ")[1];
var lastNameB = b.split(" ")[1];
return lastNameA.localeCompare( lastNameB );
})
Now you can display the list in the same manner you were doing earlier.
Above method will fail if firstname or lastname has a space character in between, so maybe you can follow what @Thilo has suggested
fullnamearray.push({first: firstname, last: lastName});
fullnamearray.sort( function(a,b){
var lastNameA = a.lastName;
var lastNameB = b.lastName;
return lastNameA.localeCompare( lastNameB );
});
and display list as
for(var i = 0; i < fullnamearray.length; i++){
Name.innerHTML += '<li>' + fullnamearray[i].firstName + " " + fullnamearray[i].lastName '</li>';
}
namearray.push({first: firstname, last: lastName}). Orpush([firstname, lastname]). That way, you can sort it without splitting the string again (which may not even work for people like "Norma Jean Baker")