我多次调用一个方法:
displayA(aTotal, sectorTotalValue, templateItems);
displayB(bTotal, sectorTotalValue, templateItems);
...many more times
我想根据aTotal
,bTotal
等升序调用它们。
因此,如果bTotal > aTotal
,则希望按以下顺序调用它们:
displayB(bTotal, sectorTotalValue, templateItems);
displayA(aTotal, sectorTotalValue, templateItems);
如果bTotal < aTotal
,则希望按以下顺序调用它们:
displayA(aTotal, sectorTotalValue, templateItems);
displayB(bTotal, sectorTotalValue, templateItems);
我该怎么做?
答案 0 :(得分:1)
这是一种基于将变量放入数组并进行自定义排序的方法:
const displayCalls = [
{ total: aTotal, func: displayA },
{ total: bTotal, func: displayB },
];
//sort array to put highest total first
displayCalls.sort((call1, call2) => call2.total - call1.total);
//call each one in order
displayCalls.forEach(call => {
call.func(call.total, sectorTotalValue, templateItems);
});