我试图按dogName按字母顺序排序,但是标准数组 .sort不起作用。我甚至试过( array .dogName).sort()无济于事。我需要做什么?
var garrDog = [];
function start() {
var vName = '';
var vToys = '';
vName = prompt("Enter the dog's name (leave blank to stop):");
while (vName.length > 0) {
var valueToPush = {};
vToys = prompt("Enter number of toys " + vName + " has:");
valueToPush['dogName'] = vName;
valueToPush['dogToys'] = vToys;
garrDog.push(valueToPush);
vName = prompt("Enter the dog's name (leave blank to stop)");
}
listDogs();
}
function listDogs() {
var i = 0;
while (i < garrDog.length) {
document.getElementById('output').innerHTML += ('Dog ' + (i+1) + ' Name: ' + garrDog[i].dogName + ' No. of toys: ' + garrDog[i].dogToys + '<br />');
i++;
}
}