A
A
Artemio952022-04-01 21:44:06
Vue.js
Artemio95, 2022-04-01 21:44:06

How to display an unsorted array when sorting?

There is a code:

computed: {
        sorted() {
            const compare = (a, b) => {
                if (sortingType === '2') {
                    return b.name- a.name
                }
                if (sortingType === '3') {
                    return a.name- b.name
                }
                return 0;
            };
            return this.arr.sort(compare);
        }
    },

The user selects sorting type from the list - 2, 3 or no sorting. The problem is that when selected without sorting, computed takes an already sorted array and does not return to its original form. How to solve the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2022-04-01
@0xD34F Vue.js

But there is nothing to sort the array. You don't have to do this. NO NEED.
Make a copy of the array inside the computed property, and sort it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question