Javascript中类别的分层排序

时间:2014-09-16 00:27:37

标签: javascript wordpress sorting tree

我试图以分层方式对下面给出的类别进行排序,然后将它们输出到网页。我知道一个事实,我需要使用某种形式的递归,因为类别可以具有任何级别的深度。我也希望能够按字母顺序对相同深度级别的类别进行排序,我知道我可以使用someArray.sort()方法。

理想情况下,我会有类似这样的结构,每个名称都是树中的一个对象,并且它的term_id作为数组中的索引:

0 Computer Technology
0 --> Hardware
0 ----> Microprocessors
0 ----> Hard Drives
0 --> Software
0 ----> Firmware & Embedded Systems
0 ----> Operating Systems
0 ----> Web and Internet-Based Applications
0 Uncategorized

到目前为止,我已经浏览了网站上的许多问题,并且无法根据我的需求明确调整其中任何一个问题。到目前为止,我已经到了这里:

function hierarchicalSort( categories, sorted, parentID ) {
    for( var i = 0; i < categories.length; i++ ) {
        if( categories[i] === null )
        {
            continue;   // There may be more categories to sort
        }
        else if( categories[i].parent == parentID ) {
            sorted.push( categories[i] ); // Same parent ( same level )
            categories[i] = null;   // Our "Already Sorted" flag
        }
        else {
            sorted[categories[i].term_id].children = [];
            // This category has a different parent
            hierarchicalSort( categories, sorted, categories[i].term_id );
            // Find this category's children
        }
    }
}

我的类别包含以下信息:

var categories = [
    {
        term_id: 1,
        name: Uncategorized,
        parent: 0,
        count: 1
    },
    {
        term_id: 2,
        name: Hardware,
        parent: 7,
        count: 1
    },
    {
        term_id: 3,
        name: Software,
        parent: 7,
        count: 2
    },
    {
        term_id: 7,
        name: Computer Tech,
        parent: 0,
        count: 0
    },
    {
        term_id: 8,
        name: Operating Systems,
        parent: 3,
        count: 1
    },
    {
        term_id: 9,
        name: Firmware and Embedded Systems,
        parent: 3,
        count: 0
    },
    {
        term_id: 10,
        name: Web and Internet-Based Applications,
        parent: 3,
        count: 0
    },
    {
        term_id: 11,
        name: Hard Drives,
        parent: 7,
        count: 3
    },
    {
        term_id: 23,
        name: Microprocessors,
        parent: 7,
        count: 1
    }
];

1 个答案:

答案 0 :(得分:0)

我建议的解决方案:

将类别数组用作队列,并在while循环运行时检查每个类别。

/**
 * Creates a category tree structure from a list of objects.
 * 
 * @param {array} categories: The list of categories to sort into a hierarchical tree.
 * It is mainly accessed as a queue to ensure that no category is missed during each
 * generation of the tree level.
 * 
 * @param {array} sorted: The "treeified" structure of categories
 * @param {int} parentID: The ID of the parent category
 * 
 * @since 0.2.0
 * 
 * @returns {undefined}
 */
function hierarchicalSort( categories, sorted, parentID )
{
    /**
     * Term Iterator var i: Goal is to go through each category at least once
     */
    var i = categories.length;

    // This loop completes a full cycle through categories for each depth level
    while( --i >= 0 ) { 
        // Pull the category out from the sorting "queue"
        var category = categories.shift();  
        if( category.parent == parentID ) {
            sorted.push( category ); // Same parent
        }
        else {
            categories.push( category ); // Uhh, this category has a different parent...
            // Put it back on the queue for sorting at a later time
        }
    }

    // Keep going until you hit the end of the array
    for( var j = 0; typeof sorted[j] !== 'undefined'; j++ ) {
        sorted[j].children = [];

        /**
         * Find this category's children, by importing the remaining queue,
         * giving the current sorted category's children property, and the 
         * current sorted category's term_id
         */
        hierarchicalSort( categories, sorted[j].children, sorted[j].term_id );
    }
}