Tigerrrrr
Tigerrrrr

Reputation: 1386

Discord.js | How do I use .sort() to sort a collection of fetched messages

I have this example code:

.sort((a, b) => b.createdAt - a.createdAt)

I've fetched and filtered the messages in a channel (The channel has 8 messages), but filter returns a collection, and collections are not necessarily in order.

So how do I sort my messages so that the formula above works?

A screenshot of my code

(Please ignore the .first() in the screenshot above)

There's 4 messages in the collection, .first() just collects the first message, I'd like to have all four values in my embed like this:

newchannel.fetchMessages().then(messages => {
    var valuesA = messages.filter(m => m.author.id === message.author.id).first();
    var valuesB = messages.filter(m => m.author.id === message.author.id).second();
    var valuesC = messages.filter(m => m.author.id === message.author.id).third();
    var valuesD = messages.filter(m => m.author.id === message.author.id).fourth();
    const embed = {
        "fields": [
            {
                "name": "Type of code:",
                "value": valuesA.content,
                "inline": true
            },
            {
                "name": "Type of code:",
                "value": valuesB.content,
                "inline": true
            },
            {
                "name": "Type of code:",
                "value": valuesC.content,
                "inline": true
            },
            {
                "name": "Type of code:",
                "value": valuesD.content,
                "inline": true
            }
        ]
    };
}

Obviously, this isn't the whole code or the entire embed but it shows everything needed.

If you have any questions feel free to ask in the comments.

Upvotes: 0

Views: 3159

Answers (2)

Tigerrrrr
Tigerrrrr

Reputation: 1386

So after a couple days of asking for help, I ended up finding the solution by myself:

I simply just didn't use .sort() and used values[number].content to select each message.

My new working code:

newchannel.fetchMessages().then(messages => {
    var values = messages.filter(m => m.author.id === message.author.id);
    values = values.first(4);
    const embed = {
        "fields": [
            {
                "name": "Type of code:",
                "value": values[0].content,
                "inline": true
            },
            {
                "name": "Type of code:",
                "value": values[1].content,
                "inline": true
            },
            {
                "name": "Type of code:",
                "value": values[2].content,
                "inline": true
            },
            {
                "name": "Type of code:",
                "value": values[3].content,
                "inline": true
            }
        ]
    };
}

Upvotes: 0

JackRed
JackRed

Reputation: 1204

A Collection extends the type Map.

The keys in Map are ordered while keys added to object are not. Thus, when iterating over it, a Map object returns keys in order of insertion.

As opposed to an object, Map have a specific order.

The Discord.js Collection has a sort method.

Since the return type of fetchMessages is a Promise<Collection<Snowflake, Message>> when resolved, you should be able to sort by createdAt.

client.on('message', async function(msg) {
  let chan = msg.channel;
  let sortedMsgs = await chan.fetchMessages(option)
    .then(msgs => {
      msgs.sort((a, b) => b.createdAt > a.createdAt) 
    })
});

Disclaimer: I can't test this code right now. The compareFunction format is not defined in the documentation, so it's maybe not >.

Upvotes: 2

Related Questions