13

I have just started to learn VueJS 2 and the more questions I have the harder it becomes.

The idea is that I have a sample problem with an array of objects, where I want to sort the array by "votes" property, which can be dynamically updated for each separate element. I want to sort my list by votes dynamically. Thus the question is how can I do that without doing weird code.

In angular you will do something like

for candidate in candidates | orderBy: 'votes'

but in here I could of though only of something like

v-for="(value, index, key) in sorted_candidates"

where in .js I'll have

 computed : {
      sorted_candidates() {
          return this.candidates.sort((a, b) => { return b.votes - a.votes;});
      }
  }

So my question would be if there is a more elegant way to solve this problem? Note: I am sorting on object property.

1
  • 1
    Using is computed is how you do it in Vue 2. Commented Aug 21, 2017 at 2:46

1 Answer 1

6

This is already a simple enough solution, in Vue1 you can still use OrderBy but in Vue2 they suggest you opt for the current solution you currently have, you can reference on here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.