javascript - 对数组进行排序以对匹配元素进行分组但保持原始顺序
问题描述
我需要对一组对象进行排序,以便对具有相似类别的项目进行分组,但不会丢失类别的原始索引
我想按类别键排序/分组,但保持类别出现在数组中的顺序。类别没有特定的顺序。
arr = [
{
name: "Name 1",
category: "D Category"
},
{
name: "Name 2",
category: "A Category"
},
{
name: "Name 3",
category: "D Category"
},
{
name: "Name 4",
category: "G Category"
},
{
name: "Name 5",
category: "A Category"
}
];
尝试不太奏效,它最终将具有相同类别的项目放在一起但按字母顺序排列,我不想要
arr.sort((first, second) => {
const firstId = first.category;
const secondId = second.category;
if (firstId < secondId) {
return -1;
}
if (firstId > secondId) {
return 1;
}
return 0;
});
预期结果:
expectedResult = [
{
name: "Name 1",
category: "D Category"
},
{
name: "Name 3",
category: "D Category"
},
{
name: "Name 2",
category: "A Category"
},
{
name: "Name 5",
category: "A Category"
},
{
name: "Name 4",
category: "G Category"
}
];
进行 .sort 比较部分适用于相似类别在一起但最终按字母顺序排列的情况,这是我不想要的
解决方案
这是一种可能对您有用的方法。基本上,它会执行您正在尝试的操作,但会引入创建优先级映射的中间步骤。此映射将包含唯一category
值(来自原始数据)作为键,它们出现的顺序作为值。然后,您可以简单地基sort
于此逻辑。我的代码可能比我的解释更清楚:
//Original data
const arr = [{
name: "Name 1",
category: "D Category",
}, {
name: "Name 2",
category: "A Category"
}, {
name: "Name 3",
category: "D Category"
}, {
name: "Name 4",
category: "G Category"
}, {
name: "Name 5",
category: "A Category"
}];
// A priority map so we have a basis for sorting
const orderPriority = arr
.map(o => o.category)
.reduce((map, category, idx) => {
if (map[category] == null) {
map[category] = idx;
}
return map;
}, {});
// Now implement the sort based on the priorty map we created:
arr.sort((a, b) => orderPriority[a.category] - orderPriority[b.category]);
console.log(arr);
推荐阅读
- reactjs - 我可以在不使用 redux 的情况下使用 redux-form
- asp.net-mvc - 在 .net 中构建基于模块的 Web 应用程序
- ios - 重新启动视图控制器
- python - 在 Spyder 中控制 iPython 控制台的输出/分页?相当于Matlab中的更多(开/关)?
- c# - C#:如何检测是否作为库中的服务运行?
- for-loop - 如何将对象类型添加到 Enterprise Architect 活动图中的区域迭代边框?
- objective-c - 从Objective c文件返回字典到Swift?
- r - 数据为 POSIXct 时,ggplotly() 不显示 geom_vline / geom_hline
- ruby-on-rails - 为什么我使用的任何邮件程序都会打开我的默认文本编辑器而不是另一个浏览器选项卡?
- sql - 在 SQL 中使用 Sysdate 查找员工年龄