优化排序

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