Javascript对象数组排序返回显着随机的结果

时间:2013-07-18 07:45:18

标签: javascript sorting

我试图在对象数组上进行简单的排序,但它似乎返回完全随机的结果。请看看这个插件:(在FF或Chrome中)

http://plnkr.co/edit/TqoyUJV4nzvC4hAkVRkz?p=preview

    var data;
    var asc;

    function init(){
        data = [{username:"leonhaas"},{username:"0johnson"},{username:"leonlang"},{username:"0lechner"},{username:"0koller"},{username:"leonwinkler"},{username:"0schmidt"},{username:"0mueller"},{username:"0schmid"},{username:"lillyfuchs"},{username:"alexandragruber"},{username:"alexanderlechner"},{username:"alexanderpichler"},{username:"alexandraeder"},{username:"lillyreiter"},{username:"alibauer"},{username:"alexandrahall"},{username:"alexandrajohnson"},{username:"alexandrataylor"},{username:"alexandrawilliams"},{username:"lilywinkler"},{username:"alinabauer"},{username:"aliceegger"},{username:"alicesteiner"},{username:"alicewallner"},{username:"aliegger"},{username:"alifuchs"},{username:"linajohnson"},{username:"amarwhite"},{username:"alinaleitner"},{username:"alinaschmidt"},{username:"alinawood"},{username:"alischneider"}];
        outputData(data);
        asc = true;
    }

    function sortIt()
    {
        a = data.username;
        b = data.username;
        if(asc){
            data.sort(function(a,b)
            {
                return 1;
            });
        } else {
            data.sort(function(a,b)
            {
                return -1;
            });
        }

        outputData(data);
    }

    function outputData(data){
        var output = "";
        data.forEach(function (item){
            output += item.username +"<br>";
        });

        var x=document.getElementById("demo");
        x.innerHTML=output;
    }

5 个答案:

答案 0 :(得分:4)

在排序功能中,您必须比较数据对象的用户名:

function sortIt()
{
    if(asc){
        data.sort(function(a,b)
        {
            return a.username.localeCompare(b.username);
        });
    } else {
        data.sort(function(a,b)
        {
            return b.username.localeCompare(a.username);
        });
    }

    outputData(data);
}

答案 1 :(得分:1)

有几件事,你没有对数组进行排序,如果值相同则需要返回0,然后根据升序/降序排序返回-1(或1)。 /> 除此之外,您在var a函数中分配了var bsortIt,这是真的,但这些并不是您将要使用的值。 sort回调。a由于回调的参数名称相同,因此它们会掩盖较高范围的bdata.sort(function(a,b) { return a.username === b.username ? 0 : a.username >b.username ? 1 : -1; }); 变量。

考虑到所有事情,排序回调应如下所示:

sortIt

考虑到所有事情,您的代码可以完成更多工作:您在整个地方使用全局变量,{{1}}函数将重新声明每次调用时的排序回调,您是&#39;重新绑定HTML中的事件处理程序(总是最好用JS管理JS侦听器,排序单一责任原则),调用可以从实际相关调用上下文等中受益的函数... look into IIFE's to create closures作为开始,和书签MDN

答案 2 :(得分:0)

您应该根据条件从sort函数返回值,而不仅仅是1或-1。像这样......

// Inside your script.js. Line #14
if(asc){
    data.sort(function(a,b)
    {
        return a>b ? 1 : -1;
    });
} else {
    data.sort(function(a,b)
    {
        return a>b ?-1 : 1;
    });
}

答案 3 :(得分:0)

function sortIt()
    {
        a = data.username;
        b = data.username;
        if(asc){
            data.sort(function(a,b)
            {
                if(a.username.localeCompare(b.username) > 0)
                return 1;
                else
                return -1;
            });
        } else {
            data.sort(function(a,b)
            {
                if(b.username.localeCompare(a.username) > 0)
                return 1;
                else
                return -1;
            });
        }

        outputData(data);
    }

答案 4 :(得分:0)

        data = [{username: "leonhaas"},...];

        var asc = true;

        data.sort(function(a, b) {
            if (asc)
                return a.username > b.username ? 1 : -1;
            else
                return a.username > b.username ? -1 : 1;
        });

        for (var i = 0; i < data.length; i++) {
            console.log(data[i].username);
        }