Reputation: 51
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.
Upvotes: 0
Views: 85
Reputation: 36351
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)
Upvotes: 2
Reputation: 3170
// 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.
Upvotes: 1