[Vuejs]-VueJS sort/order an array of objects by a specific property

2👍

Use sort and Boolean to number type coercion like so:

return this.contacts.sort(({ isPrimary: a }, { isPrimary: b }) => b - a);

This works by comparing the numeric representations of true and false, which are 1 and 0 respectively, and based upon the return value of sort‘s callback, it returns a number which determines the place each item should be moved too.

1👍

this.contacts.sort((a, b) => { a.isPrimary ? -1 : 1 })

Leave a comment