按名称,日期和层次排序列表

时间:2017-06-08 16:27:07

标签: javascript node.js sorting

我想按以下顺序对以下列表进行排序:

  1. 首先是没有父母的对象
  2. 然后按名称
  3. 年份
  4. 按月
  5. 相关对象应直接遵循标准(2-4)。
  6. 所以我想要实现的基本上是树结构作为平面列表。

    这是我到目前为止所提出的,但无法弄清楚如何实现父关系。

    function monthYearCheck(a, b) {
        if(a.year < b.year) {
            return -1;
        } else if(a.year > b.year) {
            return 1;
        } else {
            if(a.month < b.month) {
                return -1;
            } else if(a.month > b.month) {
                return 1;
            } else {
                return 0;
            }
        }
    }
    
    var sortedArray = nonSortedArray.sort(function (a, b) {
        if(a.name === b.name) {
            return monthYearCheck(a, b);
        } else if(a.name < b.name) {
            return -1;
        } else if(a.name > b.name) {
            return 1;
        }
    });   
    

    这是排序前的数组:

    [{
        name: "Bottom",
        id: 4,
        parentId: 54,
        month: 6,
        year: 2016,
        value: 16       
    },{
        name: "Topp",
        id: 12,
        month: 6,
        year: 2016,
        value: 24
    },{
        name: "Middle",
        id: 54,
        parentId: 12,
        month: 5,
        year: 2016,
        value: 14   
    },{
        name: "Middle2",
        id: 118,
        parentId: 104,
        month: 6,
        year: 2016,
        value: 4    
    },{
        name: "Bottom2",
        id: 5,
        parentId: 54,
        month: 5,
        year: 2016,
        value: 12
    },{
        name: "Topp",
        id: 12,
        month: 5,
        year: 2016,
        value: 12
    },{
        name: "Middle",
        id: 54,
        parentId: 12,
        month: 6,
        year: 2016,
        value: 4    
    },{
        name: "Topp2",
        id: 104,
        month: 5,
        year: 2016,
        value: 12
    },{
        name: "Bottom2",
        id: 5,
        parentId: 54,
        month: 6,
        year: 2016,
        value: 2        
    }]
    

    这就是我想要实现的目标:

    [{
        name: "Topp",
        id: 12,
        month: 5,
        year: 2016,
        value: 12
    },{
        name: "Topp",
        id: 12,
        month: 6,
        year: 2016,
        value: 24
    },{
        name: "Middle",
        id: 54,
        parentId: 12,
        month: 5,
        year: 2016,
        value: 14   
    },{
        name: "Middle",
        id: 54,
        parentId: 12,
        month: 6,
        year: 2016,
        value: 4    
    },{
        name: "Bottom",
        id: 4,
        parentId: 54,
        month: 6,
        year: 2016,
        value: 16       
    },{
        name: "Bottom2",
        id: 5,
        parentId: 54,
        month: 6,
        year: 2016,
        value: 2        
    },{
        name: "Bottom2",
        id: 5,
        parentId: 54,
        month: 5,
        year: 2016,
        value: 12
    },{
        name: "Topp2",
        id: 104,
        month: 5,
        year: 2016,
        value: 12
    },{
        name: "Middle2",
        id: 118,
        parentId: 104,
        month: 6,
        year: 2016,
        value: 4    
    }]
    

2 个答案:

答案 0 :(得分:1)

我建议使用树结构来获取依赖项,然后使用有序结果。

var data = [{ name: "Bottom", id: 4, parentId: 54, month: 6, year: 2016, value: 16 }, { name: "Topp", id: 12, month: 6, year: 2016, value: 24 }, { name: "Middle", id: 54, parentId: 12, month: 5, year: 2016, value: 14 }, { name: "Middle2", id: 118, parentId: 104, month: 6, year: 2016, value: 4 }, { name: "Bottom2", id: 5, parentId: 54, month: 5, year: 2016, value: 12 }, { name: "Topp", id: 12, month: 5, year: 2016, value: 12 }, { name: "Middle", id: 54, parentId: 12, month: 6, year: 2016, value: 4 }, { name: "Topp2", id: 104, month: 5, year: 2016, value: 12 }, { name: "Bottom2", id: 5, parentId: 54, month: 6, year: 2016, value: 2 }]
    .sort(function (a, b) {
        return a.name.localeCompare(b.name) || a.year - b.year || a.month - b.month;
    }),
    tree = function (data, root) {
        var r = [],
            o = {};
        data.forEach(function (a) {
            var temp = { data: [a], children: o[a.id] && o[a.id].children };
            if (o[a.id] && o[a.id].data) {
                o[a.id].data.push(a);
                return;
            }
            o[a.id] = temp;
            if (a.parentId === root) {
                r.push(temp);
            } else {
                o[a.parentId] = o[a.parentId] || {};
                o[a.parentId].children = o[a.parentId].children || [];
                o[a.parentId].children.push(temp);
            }
        });
        return r;
    }(data, undefined),
    result = function getValue(array) {
        return array.reduce(function (r, o) {
            return r.concat(o.data, getValue(o.children || []));
        }, []);
    }(tree);

console.log(result);
console.log(tree); // just how it works ...
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:0)

您可以使用以下内容:

var sortedArray = nonSortedArray.sort(function (a, b) {
    return compareParentID(a.parentId, b.parentID) || a.name.localeCompare(b.name) || a.year.localeCompare(b.year) || a.month.localeCompare(b.month);
});

function compareParentID(a, b) {
    if (a == null && b != null) 
        return -1;

    if (a != null && b == null) 
        return 1;

    return a - b;
}

如果名称元素不相等,则按名称对它们进行排序。

这会比较每一列。如果名称元素相等,则继续比较年元素等。

为您要比较的每个额外元素添加更多||子句,按您想要比较的顺序排列。