您能否建议我根据部分名称按优先级对以下数组进行排序的最佳方法。我更担心时间复杂度,因为我的数组实际上包含 100 000 条记录。
如果有更好的存储方式,我也可以更改数组结构
[{
id: 'field1',
sections: [{
name: 'Top_Section',
priority: 3
},
{
name: 'Bottom_Section',
priority: 3
}
]
},
{
id: 'field2',
sections: [{
name: 'Top_Section',
priority: 2
},
{
name: 'Bottom_Section',
priority: 4
}
]
},
{
id: 'field3',
sections: [{
name: 'Top_Section',
priority: 1
},
{
name: 'Bottom_Section',
priority: 1
}
]
},
{
id: 'field4',
sections: [{
name: 'Top_Section',
priority: 4
},
{
name: 'Bottom_Section',
priority: 2
}
]
}
];
就像我想根据 Top_Section 对优先级进行排序一样,所以我的预期输出应该如下所示,因为 field3 的优先级为 1,而 field2 的优先级为 2,依此类推。
[
{
id: 'field3',
sections: [
{ name: 'Top_Section', priority: 1 },
{ name: 'Bottom_Section', priority: 1 }
]
},
{
id: 'field2',
sections: [
{ name: 'Top_Section', priority: 2 },
{ name: 'Bottom_Section', priority: 4 }
]
},
{
id: 'field1',
sections: [
{ name: 'Top_Section', priority: 3 },
{ name: 'Bottom_Section', priority: 3 }
]
},
{
id: 'field4',
sections: [
{ name: 'Top_Section', priority: 4 },
{ name: 'Bottom_Section', priority: 2 }
]
}
];
幕布斯6054654
潇潇雨雨
相关分类