优化了排序的代码

main
Ryan 2 months ago
parent e152896d51
commit 8edc22ab16

@ -47,7 +47,7 @@ document.getElementById('callButton').addEventListener('click', function () {
// 计算总积分 // 计算总积分
let totalPoints = eligibleStudents.reduce((acc, student) => { let totalPoints = eligibleStudents.reduce((acc, student) => {
let pointsKey = Object.keys(student).find(key => key.includes('积分')); let pointsKey = Object.keys(student).find(key => key.includes('积分'));
return acc + (pointsKey ? parseInt(student[pointsKey]) : 0); return acc + (pointsKey ? parseFloat(student[pointsKey]) : 0);
}, 0); }, 0);
// 确保总积分不为零 // 确保总积分不为零
@ -72,7 +72,7 @@ document.getElementById('callButton').addEventListener('click', function () {
// 计算每个学生被选中的概率(总积分越高,被点到的概率越低) // 计算每个学生被选中的概率(总积分越高,被点到的概率越低)
let probabilities = eligibleStudents.map(student => { let probabilities = eligibleStudents.map(student => {
let pointsKey = Object.keys(student).find(key => key.includes('积分')); let pointsKey = Object.keys(student).find(key => key.includes('积分'));
let studentPoints = pointsKey ? parseInt(student[pointsKey]) : 0; let studentPoints = pointsKey ? parseFloat(student[pointsKey]) : 0;
return 1 / (studentPoints + 1) / totalPoints; return 1 / (studentPoints + 1) / totalPoints;
}); });
@ -229,7 +229,7 @@ document.getElementById('sortByPointsButton').addEventListener('click', function
updatedData.sort((a, b) => { updatedData.sort((a, b) => {
let pointsKeyA = Object.keys(a).find(key => key.includes('积分')); let pointsKeyA = Object.keys(a).find(key => key.includes('积分'));
let pointsKeyB = Object.keys(b).find(key => key.includes('积分')); let pointsKeyB = Object.keys(b).find(key => key.includes('积分'));
return (pointsKeyB ? parseInt(b[pointsKeyB]) : 0) - (pointsKeyA ? parseInt(a[pointsKeyA]) : 0); return (pointsKeyB ? parseFloat(b[pointsKeyB]) : 0) - (pointsKeyA ? parseFloat(a[pointsKeyA]) : 0);
}); });
var displayDiv = document.getElementById('excelDataDisplay'); var displayDiv = document.getElementById('excelDataDisplay');
displayDiv.innerHTML = '<div style="display: flex; justify-content: space-between;"><span>学号</span><span>姓名</span><span>积分</span></div>'; displayDiv.innerHTML = '<div style="display: flex; justify-content: space-between;"><span>学号</span><span>姓名</span><span>积分</span></div>';

Loading…
Cancel
Save