2014-09-16 2 views
0

아래에 주어진 카테고리를 계층 적 방식으로 정렬하려고 시도하고 나중에 웹 페이지로 출력하려고합니다. 카테고리가 어떤 수준의 깊이도 가질 수 있기 때문에 어떤 형태의 재귀를 사용해야한다는 사실을 알고 있습니다. 나는 알파벳 순서에 따라 동일한 깊이 수준에서 해당 범주를 정렬 할 수 있기를 원하며 이는 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 
    } 
]; 
+0

"플랫"구조를 자바 스크립트 객체의 트리로 먼저 변환합니다. 그런 다음 표준 방법을 사용하여 각 노드에 인덱스 배열을 배치 할 수 있습니다. – BadZen

+0

계층 적 데이터를 저장하는 데 사용하는 것이 확실하지 않지만 [계보 열] (http://www.ferdychristant.com/blog//articles/DOMM-7QJPM7)을 사용하면 계층 구조와 순서를 모두 적용 할 수 있습니다. : [데모] (http://jsfiddle.net/p7v7n1zg/1/). –

+0

@ Brandon-Boone : [WordPress 데이터베이스] (http://codex.wordpress.org/Database_Description#Table_Overview) 테이블 일뿐입니다. –

답변

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); 
    } 
}