优化排序

This commit is contained in:
yj 2024-08-19 09:34:26 +08:00
parent 076b6021c8
commit 69abfc81c1
1 changed files with 27 additions and 15 deletions

View File

@ -347,26 +347,38 @@ const Meeting: React.FC = () => {
}, [recorder])
const changeAgoraDevice = () => {
function moveAdminsAndSpeakersToFront(arr: any[]): any[] {
// 创建一个新的数组来存放管理员和发言人
const adminsAndSpeakers = [] as any;
// 创建一个新的数组来存放其他用户
const otherUsers = [] as any;
// 遍历原始数组
arr.forEach((item: any) => {
if (item.roleId === '1' || item.isRoomManager) {
// 如果是管理员或发言人,添加到 adminsAndSpeakers 数组
adminsAndSpeakers.push(item);
function sortUsersByRole(arr: any[]): any[] {
// 使用 sort 方法对数组进行排序
return arr.sort((a: any, b: any) => {
// 定义角色优先级
const rolePriority: any = {
admin: 1,
speaker: 2,
user: 3,
};
// 获取角色优先级
const aPriority = rolePriority[a.role] || 3; // 默认为普通用户
const bPriority = rolePriority[b.role] || 3; // 默认为普通用户
// 比较角色优先级
if (aPriority < bPriority) {
return -1; // a 的优先级更高
} else if (aPriority > bPriority) {
return 1; // b 的优先级更高
} else {
// 否则,添加到 otherUsers 数组
otherUsers.push(item);
// 如果角色相同,则可以按其他标准排序,例如按姓名字母顺序
return a.name.localeCompare(b.name);
}
});
// 将管理员和发言人放在数组的最前面
return [...adminsAndSpeakers, ...otherUsers];
}
setRoomUserList((res: any) => {
res.forEach((item: any) => {
if (item.roleId === '1') {
item.role = 'admin'
} else if (item.isRoomManager) {
item.role = 'speaker'
} else {
item.role = 'user'
}
if (item.uid === userInfo.uid) {
const footerListTemplate = [...footerList]
footerListTemplate[0][0].title = item.enableMicr ? '静音' : '解除静音'
@ -387,7 +399,7 @@ const Meeting: React.FC = () => {
item.isShow = true;
}
});
return moveAdminsAndSpeakersToFront(res)
return sortUsersByRole(res)
})
}
// 替换数据