I am getting following error TypeError: Cannot read property 'sort' of undefined when I am fetching the data from API and I want to sort and display the data, my sort function works with local static data but not with endpoint.
Below is the function for sorting
dynamicSort(property) {
var sortOrder = 1;
if(property[0] === "-") {
sortOrder = -1;
property = property.substr(1);
}
return function (a,b) {
if(sortOrder == -1){
return b[property].localeCompare(a[property]);
}else{
return a[property].localeCompare(b[property]);
}
}
}
Below is my render method
render() {
let data = this.props.brands.all_brands
data.sort(this.dynamicSort("name"));
console.log(data);
}
Below is my JSON Format
{
"all_items": [
{"name": "Banana"},
{"name": "Cat"},
{"name": "Apple"}
]
}
this.props.brandsdoes not contain anall_brandsproperty