I am Fetching the data from Server and I am having two Objects with "name" and "Money" in the array Now I need to sort the array by comparing with money field, After trying with below code I am getting error like "TypeError: Cannot read property 'money' of undefined" , somewhere I facing problem with If condition,
const [dataObject, setdataObject] = useState([{}])
const sortbyRich=()=>{
for(let i=0;i<dataObject.length;i++){
for(let j=0;j<dataObject.length;j++){
if(dataObject[j].money < dataObject[j+1].money){
let temp = dataObject[j];
dataObject[j]= dataObject[j+1];
dataObject[j+1]= temp;
}
}
}
setdataObject(dataObject);
}