【发布时间】:2020-02-19 10:02:32
【问题描述】:
我想按next_id 值对双向链表进行排序。
我的 DLL:
const dll = [
{id: '22', prev_id: '41', next_id: '45'},
{id: '45', prev_id: '22', next_id: null},
{id: '41', prev_id: '14', next_id: '22'},
{id: '14', prev_id: null, next_id: '41'},
]
结果:
const dll_result = [
{id: '14', prev_id: null, next_id: '41'}, // next item - 41
{id: '41', prev_id: '14', next_id: '22'}, // next item - 22
{id: '22', prev_id: '41', next_id: '45'}, // next item - 45
{id: '45', prev_id: '22', next_id: null},
]
我知道对 DLL 进行排序可能没有意义,但在我的情况下,我需要使用 next_id 顺序可视化数组中的数据。
P.S. 很高兴知道一个原生解决方案,然后我可以尝试自己转换为 Ramda.js
【问题讨论】:
标签: javascript arrays sorting doubly-linked-list ramda.js