javascriptdiscord.jsleaderboard

Javascript discord.js leaderboard


So right now I am trying to make an XP leaderboard but I am pretty stuck. I was able to get it where it will put the XP is put in order but I can't figure out how to also pass the other information in order with the XP like username and more. I honestly may be going in a completely wrong direction. My code is this right now is:

        let obj = {
            '737903775691309127': {
                authorid: '737903775691309127',
                username: 'Vibe Development',
                serverID: '789259215868395552',
                xp: 391,
                level: 1,
                message: 50,
                avatarURL: 'https://cdn.discordapp.com/avatars/737903775691309127/91fa0efafb801dcbea4a669a67c6602e.webp'
            },
            '455139054464270345': {
                authorid: '455139054464270345',
                username: 'Crispy Cream',
                serverID: '789259215868395552',
                xp: 753,
                level: 2,
                message: 99,
                avatarURL: 'https://cdn.discordapp.com/avatars/455139054464270345/a_9ab406e25e688594f8316909f0c470b5.gif'
            },
            '738418475801772072': {
                authorid: '738418475801772072',
                username: 'Starpower11',
                serverID: '789259215868395552',
                xp: 13,
                level: 1,
                message: 1,
                avatarURL: 'https://cdn.discordapp.com/avatars/738418475801772072/a577da70ee54c036643490a6928b026e.webp'
            },
            '270904126974590976': {
                authorid: '270904126974590976',
                username: 'Dank Memer',
                serverID: '789259215868395552',
                xp: 8,
                level: 1,
                message: 1,
                avatarURL: 'https://cdn.discordapp.com/avatars/270904126974590976/d60c6bd5971f06776ba96497117f7f58.webp'
            },
            '838650739403390976': {
                authorid: '838650739403390976',
                username: 'Vibe Testing',
                serverID: '789259215868395552',
                xp: 13,
                level: 1,
                message: 1,
                avatarURL: 'https://cdn.discordapp.com/avatars/838650739403390976/930052efdecd2dd4ef7fd2a636949979.webp'
            },
            '432129282710700033': {
                authorid: '432129282710700033',
                username: 'Aero',
                serverID: '789259215868395552',
                xp: 14,
                level: 1,
                message: 1,
                avatarURL: 'https://cdn.discordapp.com/avatars/432129282710700033/8323250fe10eef177c35288e0b3b9bb8.webp'
            },
            '280497242714931202': {
                authorid: '280497242714931202',
                username: 'Yui',
                serverID: '789259215868395552',
                xp: 3,
                level: 1,
                message: 1,
                avatarURL: 'https://cdn.discordapp.com/avatars/280497242714931202/d1f617030b44cbc64cff2269da2c5e0c.webp'
            },
            '434556304661544960': {
                authorid: '434556304661544960',
                username: 'Waifu',
                serverID: '789259215868395552',
                xp: 29,
                level: 1,
                message: 3,
                avatarURL: 'https://cdn.discordapp.com/avatars/434556304661544960/43e86c35225c23d46ae7db54bb81b8f8.webp'
            },
            '632293976661164052': {
                authorid: '632293976661164052',
                username: 'Paisley Park',
                serverID: '789259215868395552',
                xp: 13,
                level: 1,
                message: 2,
                avatarURL: 'https://cdn.discordapp.com/avatars/632293976661164052/55146c900a44874dcb75d13df16ceb68.webp'
            },
            '368362411591204865': {
                authorid: '368362411591204865',
                username: 'Melijn',
                serverID: '789259215868395552',
                xp: 8,
                level: 1,
                message: 2,
                avatarURL: 'https://cdn.discordapp.com/avatars/368362411591204865/9326b331e0e42f185318bb305fdaa950.webp'
            },
            '716293342740348948': {
                authorid: '716293342740348948',
                username: 'Pokedi',
                serverID: '789259215868395552',
                xp: 32,
                level: 1,
                message: 3,
                avatarURL: 'https://cdn.discordapp.com/avatars/716293342740348948/2ef9cd6588c333a3299f694a23e4fa4d.webp'
            }
        };

        const res = await Object.values(obj);
        var levels = [];
        for (let index = 0; index < res.length; index++) {

            levels.push(res[index].xp);
        }
        levels.sort((a, b) => b - a);
        console.log(levels)
       // [ 753, 391, 32, 29, 14, 13,  13, 13,  8,  8, 3 ]

The Object is:

let obj = {
            'userid': {
                authorid: 'userid',
                username: 'username',
                serverID: 'server id',
                xp: 391,
                level: 1,
                message: 50,
                avatarURL: 'users avatar'
            },

What I would like to get too is the array that is returned looks like

            [
                {
                    authorid: '455139054464270345',
                    username: 'Crispy Cream',
                    serverID: '789259215868395552',
                    xp: 753,
                    level: 2,
                    message: 99,
                    avatarURL: 'https://cdn.discordapp.com/avatars/455139054464270345/a_9ab406e25e688594f8316909f0c470b5.gif'
                }, 
                {
                    authorid: '737903775691309127',
                    username: 'Vibe Development',
                    serverID: '789259215868395552',
                    xp: 391,
                    level: 1,
                    message: 50,
                    avatarURL: 'https://cdn.discordapp.com/avatars/737903775691309127/91fa0efafb801dcbea4a669a67c6602e.webp'
                },
                ... and so on 
            ]

Hopefully, this all makes sense. Thank you!


Solution

  • To get an array of objects sorted by a property (in this case xp), rather than just the property itself, you should use Array.sort directly on the xp property without first creating the levels array. Something like:

    const sortedObjects = 
        Object.values(obj) // get an array of the objects themselves
        .sort((a, b) =>
            b.xp - a.xp // sort by xp from greatest to least
        );
    

    or just:

    const sortedObjects = Object.values(obj).sort((a, b) => b.xp - a.xp)
    

    would return an array of objects sorted by their xp property.