A
A
Alex9312022-03-29 20:34:49
JavaScript
Alex931, 2022-03-29 20:34:49

How to sort by id in Javascript?

Hello. There is such a code

computed: {
    sortedPosts() {
        return [...this.posts].sort((post1, post2) => post1[this.selectedSort]?.localeCompare(post2[this.selectedSort]))
},
}

and such
((post1, post2) => post1.id - post2.id)
I want to add sorting by id. I don't understand why the code doesn't work. I do it like this.
computed: {
    sortedPosts() {
        return [...this.posts].sort((post1, post2) => post1[this.selectedSort]?.localeCompare(post2[this.selectedSort])?.(post1, post2) => post1.id - post2.id)
}
}

And I also tried that
computed: {
    sortedPosts() {
        return [...this.posts].sort((post1, post2) => post1[this.selectedSort]?.localeCompare(post2[this.selectedSort]):post1.id - post2.id)
}
}

Tell me what I'm doing wrong?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question