1

I am trying to get a simple sort working on an object array but it seems to be returning completely random results. Please have a look at this plunk: (in FF or 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;
    }
4

5 回答 5

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);
}
于 2013-07-18T07:53:39.703 回答
1

有几件事,您没有对数组进行排序,0如果值相同,则需要返回,然后根据升序/降序排序返回 -1(或 1)。
除此之外,你在你的函数中赋值var a,确实如此,但这些不是你将在回调中使用的值。因为回调的参数是同名的,所以它们屏蔽了更高范围的和变量。var bsortItsortab

考虑到所有因素,排序回调应如下所示:

data.sort(function(a,b)
{
    return a.username === b.username ? 0 : a.username >b.username ? 1 : -1;
});

考虑到所有因素,您的代码可以做更多的工作:您在所有地方都使用全局变量,您的sortIt函数将在每次调用时重新声明排序回调,您在 HTML 中绑定事件处理程序(总是最好管理 JS JS 中的侦听器,类似于单一责任原则),调用可以从实际相关调用上下文中受益的函数等...查看 IIFE 以创建闭包作为开始,并为 MDN 添加书签

于 2013-07-18T07:55:59.160 回答
0

您应该根据条件从排序函数返回值,而不仅仅是 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;
    });
}
于 2013-07-18T07:50:52.890 回答
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);
        }
于 2013-07-18T08:02:19.330 回答
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);
    }
于 2013-07-18T07:56:18.883 回答