How can I make my leaderboard descending?

Ok, so my code is:

        <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 === `${BoostData.guildID}/null`) { %>
                    <img class="serverIcon"
                    <% } else { %>
                    <img class="serverIcon" src="<%= BoostData.guildIcon %>">
                    <% } %>
                    <a href="/info/<%= BoostData.guildID %>" class="viewServer">View Server</a>
                    <% }); %>

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.


I tried doing:

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



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

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


Just fixed it by doing:

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


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


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?


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.


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