使用多个对象对数组进行排序

时间:2017-05-15 03:38:12

标签: javascript arrays sorting object

我试图按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++;
    }
 }

0 个答案:

没有答案