How can I make my leaderboard descending?

Ok, so my code is:

<ul>
        <tbody id="myTable">
            <% data.forEach(function(BoostData) { %>

            <a href="/info/<%= BoostData.guildID %>"><strong><%= BoostData.guildName %></strong></a>
            <p>Boosts: <b><%= BoostData.boostAmount %></b>

                <% if(BoostData.serverDescription === "None") { %>
                <% } else {%>
                <p class="centerServerDesc"><%= BoostData.serverDescription %></p>
                <% } %>

                <% if(BoostData.serverInvite === "None") { %>
                <p><b>No server invite</b></p>
                <% } else { %>
                <a href="https://<%= BoostData.serverInvite %>">
                    <p><b>Server Invite</b></p>
                    <% } %>

                    <% if(BoostData.guildIcon === `https://cdn.discordapp.com/icons/${BoostData.guildID}/null`) { %>
                    <img class="serverIcon"
                        src="https://images-ext-2.discordapp.net/external/ouGhEoGzz1ZyBG9mMFrYClvdv9V0FZ0jGSEHa_kLLYk/https/discordapp.com/assets/0e291f67c9274a1abdddeb3fd919cbaa.png">
                    <% } else { %>
                    <img class="serverIcon" src="<%= BoostData.guildIcon %>">
                    <% } %>
                    <a href="/info/<%= BoostData.guildID %>" class="viewServer">View Server</a>
                    <br><br><br>
                    <% }); %>
        </tbody>
    </ul>

And I want to sort everything by the BoostData.boostAmount. The highest server that’s boosted will be at the top. How can I do this? Im getting all my data from mongodb and it’s not in an array.

Probably you should sort on server side like,

let sorted = data.sort((a, b) => b.boostAmount - a.boostAmount);
// note that I reversed the order of var a, b inside fn, to sort in DESC.

Search for “sort array in js” if you want details

Sadly, my data isn’t in an array.

Then how are you executing forEach method, if it’s not an array?

1 Like

Lemme show you some of the server side code.

let boostdata = await Boost.find({
      lb: "all"
    }, (err, data) => {
      if (err) console.log(err)
    })

    res.render('pages/leaderboard', {
      data: boostdata,
    })

The model:

const mongoose = require('mongoose');

const boostSchema = mongoose.Schema({
    guildID: String,
    guildName: String,
    boostAmount: Number,
    cooldown: Number,
    lb: {
        type: String,
        default: "all"
    },
    guildIcon: String,
    serverInvite: String,
    serverDescription: String,
    imageOne: String,
    imageTwo: String,
    imageThree: String
})

module.exports = mongoose.model("Boosts", boostSchema)

@HK420 - Can you check log of typeof boostdata and "length" in data just before res.render()
I guess it should be array, if you can do forEach…

The length is 0 rn because I don’t have any data in my mongodb rn.

What the forEach is doing is its going through all the data for each guild and displaying it…

Also, it is an array but inside the array is json data.

Hmmm,

I tried doing:

boostdata.forEach(function(BoostData) {
      boostDataArr = []

      boostDataArr.push(BoostData.boostAmount)

      console.log(boostDataArr)
    })

And I added data from two servers but now im getting:
image

(Each server has one boost, that’s why both values are 1.)

UPDATE:

Just fixed it by doing:

boostDataArr = []
    boostdata.forEach(function(BoostData) {
      boostDataArr.push(BoostData.boostAmount)
    })
    console.log(boostDataArr)
1 Like

@vrintle

var boostDataArr = []
    boostdata.forEach(function(BoostData) {
      boostDataArr.push(BoostData.boostAmount)
    })
    let test = await boostDataArr.sort(function(a, b){return b - a})

    console.log(test)

I have this on my server side. Now, how would I add it to my ejs?

@HK420 - I would suggest you to add BoostData as whole, as I guess you’ll render other props on frontend. Then, you have to sort like I did before.

I think it would be same as,

res.render('pages/leaderboard', {
    data: test,
})

@vrintle what would I write in my ejs file?

Can anyone else help?

I was never very good at reading mongoose schemas - can you provide a sample of the data stored in your database?

@CarlyRaeJepsenStan

In <% data.forEach... you have to reverse the data, so just add .reverse() after data

Well… it reversed the data but I want to order the boostAmount from greatest to least.

Welp, I figured out how to do it…

<% data.sort((a, b) => b.boostAmount - a.boostAmount).forEach(function(BoostData) { %>

Added this in the foreach.

2 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.