I have this below list, getting data from api :
const Studentdata = {
"StudentName1": {
"active": true,
"gender": "male"
},
"StudentName2": {
"active": false,
"gender": "male"
},
"StudentName3": {
"active": false,
"gender": "female"
},
"StudentName4": {
"active": true,
"gender": "female"
},
"StudentName5": {
"active": true,
"gender": "female"
},
"StudentName6": {
"active": false,
"gender": "female"
}
}
and I render this data like this:
Object.keys(Studentdata).map(item => {
return (
<>
<span>{item}</span>
// will use {Studentdata[item]} list to render ul,li
)
})
So, here I Want to filter student data based on is she/he is active, active true items should come first. Expected op is:
const Studentdata = {
"StudentName1": {
"active": true,
"gender": "male"
},
"StudentName4": {
"active": true,
"gender": "male"
},
"StudentName5": {
"active": true,
"gender": "female"
},
"StudentName2": {
"active": false,
"gender": "female"
},
"StudentName3": {
"active": false,
"gender": "female"
},
"StudentName6": {
"active": false,
"gender": "female"
}
}
I am using it in React js, So I need to exact format to render / re-render the DOM. Because, I need this "StudentName1" to displan student name under this I Will show his details. I'm able to get the below:
Object.values(Studentdata).sort((a, b) => b.active - a.active)
but this gives me:
[0: {active: true, gender: "male"},
1: {active: true, gender: "female"},
2: {active: true, gender: "female"},
3: {active: false, gender: "male"},
4: {active: false, gender: "female"}]
But if I render this I need to change the render method, So need the format I mentioned above.Thanks