[Vuejs]-Sorting array coming from computed property, in a method also sorts the original array (that comes from the computed property)

1👍

Well, you first sort the array in bestResults(), then use the sorted array in recentResults.

As a solution, you can create a new array with the same elements and sort that, which will leave the original array untouched:

 bestResults() {
    let orderedArray = [...this.allResults];

    orderedArray.sort((a, b) =>
      a.score < b.score ? 1 : a.score > b.score ? -1 : 0
    );
    let half = Math.round(orderedArray.length / 2);

    let bestResults = orderedArray.slice(0, half);
    return bestResults;
  },

 recentResults() {
    let recentResults = this.allResults.slice(0, 5);
    return recentResults;
  }
👤fjc

Leave a comment