var array = [
{
"name": "Item1",
"section": "section1",
"total": 3,
}, {
"name": "Item1",
"section": "section2",
"total": 4,
},{
"name": "Item1",
"section": "section3",
"total": 7,
}, {
"name": "Item2",
"section": "section1",
"total": 1,
}, {
"name": "Item2",
"section": "section2",
"total": 2,
}, {
"name": "Item2",
"section": "section3",
"total": 3,
}
];
array = array.sort((o1, o2)=>{
if(o1.section === o2.section && o1.section === 'section3') {
return o1.total - o2.total;
} else {
return o1.section === 'section3' ? 1 : -1;
}
});
console.log(array);
Вот вывод
[
{
"name": "Item1",
"section": "section1",
"total": 3
},
{
"name": "Item1",
"section": "section2",
"total": 4
},
{
"name": "Item2",
"section": "section1",
"total": 1
},
{
"name": "Item2",
"section": "section2",
"total": 2
},
{
"name": "Item2",
"section": "section3",
"total": 3
},
{
"name": "Item1",
"section": "section3",
"total": 7
}
]