对数组函数进行排序

时间:2016-10-29 11:39:15

标签: javascript arrays sorting

图片 My program

我制作一个节目你是用不同的苏打水写的,并用积分给它们评分,我很难在苏打水给出多少分之后对它进行排序。

HTML

<!DOCTYPE html>
<html>
<head>

    <script src = "Assignment08.js"></script>
    <meta charset = "utf-8">
</head>

<body>
    <h1>Innlevering 8</h1>
    <img src="julebrus.jpg" alt="Julebrus" style="width:304px;height:228px;"><br>

    Brustype: <input id = "brustype" /><br>
    Poeng:    <input id = "poeng" /><br>

    <input type = "button" value = "Legg til brus" onclick = "leggTilBrus()" />

    <b><br>Liste over brus:</b>
    <div id = "brusliste"></div>

</body>
</html>

的Javascript

var brusliste = [];

function Brus(sodatype, points) { // Brus = Soda (English)
    this.brustype = sodatype;
    this.poeng = points;

    this.sodainfo = function() {
        return this.brustype + " " + this.poeng;
    }
}

function leggTilBrus(){
    var sodatype = document.getElementById("brustype").value;
    var points = document.getElementById("poeng").value;

    var sodas = new Brus (sodatype, points);

    brusliste.push(sodas);
    visArray();
}

function sortFunction(tall1,tall2){
    return tall1.verdi-tall2.verdi;
}

function visArray() {
    document.getElementById("brusliste").innerHTML = "";

    for (var i = 0; i < brusliste.length; i++){
        document.getElementById("brusliste").innerHTML += brusliste[i].sodainfo() + "<br />";
    }
}

function init() {
    var b = new Brus(" ", " ");

    console.log(b);
    console.log(b.sodainfo());

    document.getElementById("brusliste").innerHTML =  b.brustype + " <br />";

    brusliste.sort(sortFunction);
    //brusliste.sort(sorteringsFunksjon());
}

window.onload = init;

我需要写一些更多的帖子来接受帖子x)无论如何,任何帮助将不胜感激。这件事让我的大脑扭曲了。

1 个答案:

答案 0 :(得分:0)

我得到了它的工作

function Brus(brustype, poeng) {
    this.brustype = brustype;
    this.poeng = poeng;

    this.brusinfo = function () {
        return this.brustype + " " + this.poeng;
    };
}

var brusliste = [];

function leggTilBrus() {
    var brustype = document.getElementById("brustype").value;
    var poeng = document.getElementById("poeng").value;

    var sodas = new Brus (brustype, poeng);

    brusliste.push(sodas);
    visArray();

    document.getElementById("brustype").value = "";
    document.getElementById("poeng").value = "";
}

function sortFunction(a, b){
    return b.poeng - a.poeng;
}


function visArray() {
    brusliste.sort(sortFunction);
    document.getElementById("brusliste").innerHTML = "";

    for (var i = 0; i < brusliste.length; i++) {
        document.getElementById("brusliste").innerHTML += brusliste[i].brusinfo() + "<br />";
    }
}

function init() {
    var b = new Brus(" ", " ");

    console.log(b);
    console.log(b.brusinfo());

    document.getElementById("brusliste").innerHTML =  b.brusinfo() + " <br />";
} 

window.onload = init;

我完成了sortfunction错误,并且还缺少两个getElementById。这个区域:

        document.getElementById("brustype").value = "";
        document.getElementById("poeng").value = "";
    }

    function sortFunction(a, b){
        return b.poeng - a.poeng;
    }