如何根据widgetOrder数组的sort_number重排widgets数组?
依据自定义规则重排仪表盘Widget数组的实现问题
场景说明
- 对应功能:管理后台仪表盘的可自定义展示位置、顺序的Widget卡片块
- 存储逻辑:当用户调整某一卡片的位置后,对应的配置信息会被推入
widgetOrder数组持久化 - 待实现目标:按照
widgetOrder数组中存储的sort_number规则,对全量widgets数组完成重排
踩坑记录
此前尝试通过遍历widgets数组、逐一对标widgetOrder中对应sort_number值的方式实现排序,最终输出结果不符合预期
示例数据
const widgets = [ { id: 59, roleId: 3, widgetId: 1, sort_number: 1, created_by: '1', updated_by: '1', createdAt: '2021 - 04 - 22T03: 09: 32.098Z', updatedAt: '2021 - 04 - 22T03: 09: 32.098Z', 'dashboardWidget.id': 1, 'dashboardWidget.name': 'bookingCount', 'dashboardWidget.displayName': 'Booking Count', 'dashboardWidget.description': 'Shows the count of total bookings for the time period if specified, otherwise shows all booking count', 'dashboardWidget.isCounter': 1 }, { id: 60, roleId: 3, widgetId: 2, sort_number: 2, created_by: '1', updated_by: '1', createdAt: '2021 - 04 - 22T03: 09: 32.098Z', updatedAt: '2021 - 04 - 22T03: 09: 32.098Z', 'dashboardWidget.id': 2, 'dashboardWidget.name': 'pickUpCount', 'dashboardWidget.displayName': 'Pick-up Count', 'dashboardWidget.description': 'Shows the count of total pick-ups for the time period if specified, otherwise shows all pick-up count', 'dashboardWidget.isCounter': 1 }, { id: 61, roleId: 3, widgetId: 3, sort_number: 3, created_by: '1', updated_by: '1', createdAt: '2021 - 04 - 22T03: 09: 32.098Z', updatedAt: '2021 - 04 - 22T03: 09: 32.098Z', 'dashboardWidget.id': 3, 'dashboardWidget.name': 'dropOutCount', 'dashboardWidget.displayName': 'Drop-out Count', 'dashboardWidget.description': 'Shows the count of total drop-outs for the time period if specified, otherwise shows all drop-out count', 'dashboardWidget.isCounter': 1 }, { id: 62, roleId: 3, widgetId: 9, sort_number: 4, created_by: '1', updated_by: '1', createdAt: '2021 - 04 - 22T03: 09: 32.098Z', updatedAt: '2021 - 04 - 22T03: 09: 32.098Z', 'dashboardWidget.id': 9, 'dashboardWidget.name': 'enquiryCount', 'dashboardWidget.displayName': 'Enquiry Count', 'dashboardWidget.description': 'Shows the count of total enquires for the time period if specified, otherwise shows all enquiry count', 'dashboardWidget.isCounter': 1 } ]; const widgetOrder = [ { widgetId: 2, sort_number: 4, is_visible: 1 }, { widgetId: 3, sort_number: 1, is_visible: 1 } ];
问题根因与实现逻辑
此前排序失效的核心原因,大多是没有兼顾三类Widget的排序优先级:
- 存在于
widgetOrder配置项的Widget,必须优先使用用户自定义的sort_number作为排序依据 - 未在
widgetOrder中做过位置调整的Widget,要沿用自身原本的sort_number作为兜底排序依据 - 当两个Widget的排序键数值相同时,要保留它们在原数组中的相对顺序,避免出现位置跳变
具体实现代码:
// 提前建立widgetId到自定义排序值的映射,减少重复遍历的性能消耗 const customSortMap = new Map( widgetOrder.map(item => [item.widgetId, item.sort_number]) ); const sortedWidgets = widgets .map((widget, index) => ({ // 记录原数组索引,用于同排序值场景下的稳定排序 sourceIndex: index, // 排序键优先取自定义配置,无配置时取Widget自身的初始排序值 sortKey: customSortMap.get(widget.widgetId) ?? widget.sort_number, widgetData: widget })) .sort((a, b) => { // 优先按排序键升序排列 if (a.sortKey !== b.sortKey) return a.sortKey - b.sortKey; // 排序键一致时,按原数组先后顺序排列,保证排序稳定性 return a.sourceIndex - b.sourceIndex; }) // 提取出最终的Widget数组 .map(item => item.widgetData);
基于示例数据运行上述代码后,得到的最终数组顺序为:
- widgetId=3(自定义
sort_number=1) - widgetId=1(无自定义配置,原
sort_number=1) - widgetId=9(无自定义配置,原
sort_number=4) - widgetId=2(自定义
sort_number=4)
内容的提问来源于stack exchange,提问作者Ujjual
相关产品推荐
相关产品推荐

