通过匹配开始和结束节点对项目进行排序

时间:2017-06-27 14:19:32

标签: javascript sorting

假设我有一组属于不同群体的随机改组。例如:

let pieces = [
  {
    id: "a1",
    startNode: 18,
    endNode: 42,
  },
  {
    id: "a3",
    startNode: 16,
    endNode: 30,
  },
  {
    id: "b2",
    startNode: 48,
    endNode: 65,
  },
  {
    id: "a2",
    startNode: 42,
    endNode: 16,
  },
  {
    id: "a4",
    startNode: 30,
    endNode: 31,
  },
  {
    id: "b1",
    startNode: 23,
    endNode: 48,
  },
];

我希望它们以正确的顺序作为两个数组返回:

ordered = [
  [
    {
      id: "a1",
      startNode: 18,
      endNode: 42,
    },
    {
      id: "a2",
      startNode: 42,
      endNode: 16,
    },
    {
      id: "a3",
      startNode: 16,
      endNode: 30,
    },
    {
      id: "a4",
      startNode: 30,
      endNode: 31,
    },
  ],[
    {
      id: "b1",
      startNode: 23,
      endNode: 48,
    },
    {
      id: "b2",
      startNode: 48,
      endNode: 65,
    },
  ]
];

它们按匹配的起始和结束节点排序,因此“a2”在“a1”之后,因为它的startNode与“a1”的endNode匹配。 “b1”和“b2”属于不同的组,因为它们不与任何“a”组共享起始节点或结束节点。 id不能用于排序,这只是为了清晰起见。

关于如何做到这一点的任何想法?我认为它需要某种递归功能,我不能完全理解这一点。

2 个答案:

答案 0 :(得分:2)

尝试使用简单的Array#sort() => a.id > b.id

let pieces = [ { id: "a1", startNode: 18, endNode: 42, }, { id: "a3", startNode: 16, endNode: 30, }, { id: "b2", startNode: 48, endNode: 65, }, { id: "a2", startNode: 42, endNode: 16, }, { id: "a4", startNode: 30, endNode: 31, }, { id: "b1", startNode: 23, endNode: 48, }, ];

console.log(pieces.sort((a,b)=> a.id > b.id))

不支持的箭头功能=> ES5

一起使用
   console.log(
    pieces.sort(function(a,b){
    return a.id > b.id})
    )

答案 1 :(得分:2)

您可以使用迭代方法,同时迭代子结果以及实际(外部)元素。

此单个元素在数组中收集所有匹配的开始和结束节点,而其他非匹配节点将被过滤,稍后将收集数组连接起来。

var pieces = [{ id: "a1", startNode: 18, endNode: 42 }, { id: "a3", startNode: 16, endNode: 30 }, { id: "b2", startNode: 48, endNode: 65 }, { id: "a2", startNode: 42, endNode: 16 }, { id: "a4", startNode: 30, endNode: 31 }, { id: "b1", startNode: 23, endNode: 48 }],
    result = pieces.reduce(function (r, a) {
        var temp = [a];
        return r.filter(function (b) {
            if (temp[temp.length - 1].endNode === b[0].startNode) {
                temp = temp.concat(b);
                return;
            }
            if (temp[0].startNode === b[b.length - 1].endNode) {
                temp = b.concat(temp);
                return;
            }
            return true;
        }).concat([temp]);
    }, []);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }