I have this Array:
const list = [
{ color: 'white', size: 'XXL' },
{ color: 'red', size: 'XL' },
{ color: 'black', size: 'M' }
]
I want to sort it by the values of color or size:
colorSort = () => {
list.sort((a, b) => (a.color > b.color) ? 1 : -1)
}
sizeSort = () => {
list.sort((a, b) => (a.size> b.size) ? 1 : -1)
}
I don't like to create 2 functions for sorting.
Is there any way to create a function like this:
sortArray = (value:string = 'color') => {
#code here
}