如何为排行榜排序JSON对象?

时间:2018-02-06 20:13:06

标签: javascript jquery json

我试图用我的jquery对以下JSON进行排序,然后用他们的钱来制作一个排行榜。我希望#1领导者是最赚钱的人,最后一个人是最少的人。我已经尝试了几种方法,包括排序功能,但我似乎无法让它工作。你能解释这样做的最好方法吗?

    {
id: "3",
username: "test3",
money: "500",
state_id: "0",
state_name: "none",
state_abbrev: "none",
bets_amount_total: 0,
bets_amount_won: 0,
bets_amount_lost: 0
},
{
id: "4",
username: "johnny",
money: "500",
state_id: "0",
state_name: "none",
state_abbrev: "none",
bets_amount_total: 0,
bets_amount_won: 0,
bets_amount_lost: 0
},

这是我的代码:

    getLeaders().done(function(results){

            $.each(results, function(i, leaderStats){
                var leaderUser = leaderStats.username;
                var money = leaderStats.money

            $('#leader-container').append('\
            <div>\
            <div class="leaderboard-rank">rank</div>\
            <div class="leaderboard-name">' + leaderUser +'</div>\
            <div class="leaderboard-score">'+ money +'</div>\
            </div>\
            ');

            });
        });

0 个答案:

没有答案