0

In my current project I have to sort an array of objects based on specific sort parameters:

the order (up / down),

and the sort type(name, description, category).

Therefore I created a sort array with syntax sort = [type, order].

Now I want to sort my array of objects based on the sort type and order. My current implementation is:

  computed: {
accountsList() {
  var list = this.$store.getters["accounts/accountsList"];
  if (this.$route.query.fave) {
    list = list.filter((x) => x.fave == true);
  } else if (this.$route.query.my) {
    //my
  }
  if (this.sort.length != 0) {
    list = list.sort((a, b) => {
      if (this.sort[1] == "up") {
        if (a[this.sort[0]] < b[this.sort[0]]) {
          return 1;
        }
        if (a[this.sort[0]] > b[this.sort[0]]) {
          return -1;
        }
        return 0;
      } else {
        if (a[this.sort[0]] < b[this.sort[0]]) {
          return -1;
        }
        if (a[this.sort[0]] > b[this.sort[0]]) {
          return 1;
        }
        return 0;
      }
    });
  }
  return list;
},

My sort array is changed like this:

  changeSort(sort, order) {
      if (this.sort[0] == sort && this.sort[1] == order) {
        this.sort = [];
      } else {
        this.sort = [sort, order];
      }

My array of objects consists of:

[{ category: "test2",
description: "adadad",
name: "adadadadad"
},
{
category: "test",
description: "my description",
name: "NewAccount"
​​​},
{
category: "test",
description: "My other description",
​​​name: "another new account"
}]

At the moment it's not working properly, it's just sorting two elements and leaves the other ones untouched.

9
  • 1
    can you show sample of that of this.sort array. ? Commented Nov 9, 2020 at 8:13
  • I just added my changeSort-method to the post Commented Nov 9, 2020 at 8:16
  • 1
    Please don't use Stack Snippets for non-runnable examples, just use code blocks. Stack Snippets are for runnable examples. (A runnable example would be good, though. :-) ) Commented Nov 9, 2020 at 8:17
  • Please show the contents of the array you're trying to sort. (I think that's what @robert meant to ask for above, but they said this.sort and I think they meant list.) Commented Nov 9, 2020 at 8:17
  • The parameters sort and order are given by the <span>-tag the user clicks on. As stated above the sort-type is a parameter type the object has and the order type is either up or down. Commented Nov 9, 2020 at 8:17

2 Answers 2

1

You can use Lodash's orderBy function as the example below:

var users = [
  { 'user': 'fred',   'age': 48 },
  { 'user': 'barney', 'age': 36 },
  { 'user': 'fred',   'age': 40 },
  { 'user': 'barney', 'age': 34 }
];
 
_.orderBy(users, ['user', 'age'], ['asc', 'desc']);

For further reference: https://lodash.com/docs/4.17.15#orderBy

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

Comments

0

I would simplify Your code and ask a question - do You return list? Maybe You could supply a whole method, or even fully working example.

export {
  data() {
    list: []
  },
  methods: {
    sortList() {
      if (this.sort.length != 0) {
        let col = this.sort[0] || 'id', // default col
            dir = this.sort[1];

        this.list = this.list.sort((a, b) => {
           let colA = a[col], 
               colB = b[col];
     
           if (dir == "up") {
             return (colA  < colB ? 1 : (colA  > colB ? -1 : 0))
           } else {
             return (colA  < colB ? -1 : (colA  > colB ? 1 : 0))
           }
        });
     }
   }
}

1 Comment

yup, the sort method is in the computed-property of my vue object.

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.