And Here is the Array:
If someone's result + GMath is more than others I want to place him first in the array.
I'm making an angular app. I need to filter it for the app. If you need the template or the ts file just comment in below.
What you need to do is use sort to subtract the sum of a from the sum of b like this:
let arr = [
{result: 5, GMath: 5},
{result: 2, GMath: 8},
{result: 4, GMath: 10},
{result: 1, GMath: 1}
]
arr.sort((a, b) => (b.result + b.GMath) - (a.result + a.GMath))
console.log(arr)
// const obj = { admissionStudents: {...} }; // assuming this is the object to begin with
let keys = Object.keys(obj.admissionStudents);
keys.sort((a, b) => {
return (obj.admissionStudents[b].result + obj.admissionStudents[b].GMath) - (obj.admissionStudents[a].result + obj.admissionStudents[a].GMath);
});
Now keys will be sorted, so frame the object using this.
New list,
newList = [];
keys.forEach((key) => {
newList.push(obj.admissionStudents[key]);
});
newList will be the sorted list.
Hope it helps.
place him first in the array.That's not a filter, it s a sort.. So do you just want the array sorting by "result + GMath"?