How it works...

The computed property we just created, damsByElectricity, will return an array that will be a sorted clone of this.dams. As always with computed properties, the result will be cached (or memorized); every time we need the result, if the original list is not changed, the function will not be called and the cached result will be returned.

The sort function accepts two parameters: two members of the list. The return value must be a positive number if the second member is after the first or a negative number if the opposite is true.

The order we obtain with d2.electricity - d1.electricity is descending; if we want an ascending order, we have to commute the two operands or multiply them by -1.

Get Vue.js 2 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.