1

I need to sort my array but since I have two objects in it I am not sure how to do this. I need to sort dog names in alphabetical and toy number in ascending order. How can I do this?

function start() {
    document.getElementById("task").innerHTML="Task 8";
    var arr = [];
    var vName = "";
    vName = prompt("Enter dog name (leave blank to stop)");
    vToyNum = prompt("Enter number of dog toys (leave blank to stop)");
    while (vName.length > 0 && vToyNum.length > 0) {
        arr.push({name:vName,toynum:vToyNum});
        arr.sort({name:vName});
        vName = prompt("Enter dog name (leave blank to stop)");
        vToyNum = prompt("Enter number of dog toys (leave blank to stop)");
    }
    var vOutput = "Dog names and No. of toys:" + displayDog(arr);
    var toyTot = 0;
    for (var val=0; val < arr.length; val++) {
        toyTot += Number (arr[val].toynum);
    }
    vOutput += "<br/><br/>Total number of toys: " + toyTot;
    document.getElementById("output").innerHTML= vOutput;
}

function displayDog(arr) {
    var vOutput = "";
    for (var val = 0; val < arr.length; val++) {
        vOutput += "<br/> Dog " + (val + 1) +" "+ arr[val].name
                + ", No. of toys " + arr[val].toynum;
    }
    return vOutput;
}
Reporter
  • 3,897
  • 5
  • 33
  • 47
  • Possible duplicate of [Javascript, how do you sort an array on multiple columns?](https://stackoverflow.com/questions/2784230/javascript-how-do-you-sort-an-array-on-multiple-columns) – Andreas May 24 '17 at 09:03
  • which order do you need? name asc, toys asc ot toys asc, name asc? – Nina Scholz May 24 '17 at 09:08
  • I would like an example of name asc and another example of toys asc so one of each –  May 24 '17 at 09:10

1 Answers1

0

You could use a sort function as Array#sort requires for custom sorts and sort by name first and then by toynum

arr.sort(function (a, b) {
    return a.name.localeCompare(b.name) || a.toynum - b.toynum;
});

You need to sort only once after filling the array with data.

function start() {
    document.getElementById("task").innerHTML = "Task 8";
    var arr = [],
        vName = "",
        vToyNum,
        vOutput,
        toyTot = 0,
        val,
        DOG_QUESTION = "Enter dog name (leave blank to stop)",
        TOY_QUESTION = "Enter number of dog toys (leave blank to stop)";

    vName = prompt(DOG_QUESTION);
    vToyNum = prompt(TOY_QUESTION);
    while (vName.length > 0 && vToyNum.length > 0) {
        arr.push({ name: vName, toynum: vToyNum });
        vName = prompt(DOG_QUESTION);
        vToyNum = prompt(TOY_QUESTION);
    }

    arr.sort(function (a, b) {
        return a.name.localeCompare(b.name) || a.toynum - b.toynum;
    });

    vOutput = "Dog names and No. of toys:" + displayDog(arr);
    
    for (val = 0; val < arr.length; val++) {
        toyTot += Number(arr[val].toynum);
    }

    vOutput += "<br /><br />Total number of toys: " + toyTot;
    document.getElementById("output").innerHTML = vOutput;
}

function displayDog(arr) {
    var val, vOutput = "";
    for (val = 0; val < arr.length; val++) {
        vOutput += "<br /> Dog " + (val + 1) + " " + arr[val].name + ", No. of toys " + arr[val].toynum;
    }
    return vOutput;
}

start();
<div id="task"></div>
<div id="output"></div>
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392