簡體   English   中英

按分數排序數組,可能使用lodash。 (的NodeJS)

[英]Ordering array with hierarchy by score, possibly using lodash. (nodejs)

我有點困在這一個:我有一個根據層次結構( parent_id )排序的對象數組,如下所示:

let myArray = [
 { id: 1, parent_id: null, score: 20, type: 1 },
 { id: 12, parent_id: 1, score: 25, type: 2 },
 { id: 23, parent_id: 12, score: 55, type: 3 },
 { id: 35, parent_id: 12, score: 25, type: 3 },
 { id: 10, parent_id: null, score: 75, type: 1 },
 { id: 25, parent_id: 10, score: 15, type: 2 },
 { id: 100, parent_id: 25, score: 88, type: 3 }
]

現在我想維護層次結構順序,還要按分數對元素進行排序以獲得如下內容:

let expected = [
 { id: 10, parent_id: null, score: 75, type: 1 },
 { id: 25, parent_id: 10, score: 15, type: 2 },
 { id: 100, parent_id: 25, score: 88, type: 3 },
 { id: 1, parent_id: null, score: 20, type: 1 },
 { id: 12, parent_id: 1, score: 25, type: 2 },
 { id: 23, parent_id: 12, score: 55, type: 3 },
 { id: 35, parent_id: 12, score: 25, type: 3 },
]

我正在編寫非常低效的代碼,嵌套的foreach幾乎可以工作,但還沒有完成。 我想知道是否有更整潔的解決方案。 (很確定有,但對我來說太聰明了)。 同樣在我的代碼中我轉發了type屬性,但理想情況下我不會用它來進行排序。

注意:這個數據只是一個例子,真實數組更大,每個父項的子數變化。

由於我的解釋不是很好,我們可以用這種方式思考層次結構type:1 - >國家type:2 - >州type:3 - >城市

所以我需要通過這樣的得分desc來訂購

- Country
  - State
    - City 
    - City
  - State
    - City
 - Country and so on...

感謝願意幫助我的人,

單個排序不起作用,因為在排序數據時不遵守父子關系。

這種方法分為三個部分:

  1. score對數據排序,因為以插入順序構建了以下樹。
  2. 用給定的關系構建一棵樹。
  3. 遍歷樹並返回已排序的平面數據。

 var data = [{ id: 1, parent_id: null, score: 20, type: 1 }, { id: 12, parent_id: 1, score: 25, type: 2 }, { id: 23, parent_id: 12, score: 55, type: 3 }, { id: 35, parent_id: 12, score: 25, type: 3 }, { id: 10, parent_id: null, score: 75, type: 1 }, { id: 25, parent_id: 10, score: 15, type: 2 }, { id: 100, parent_id: 25, score: 88, type: 3 }] .sort(function (a, b) { return b.score - a.score; }), tree = function (data, root) { var r = [], o = {}; data.forEach(function (a) { o[a.id] = { data: a, children: o[a.id] && o[a.id].children }; if (a.parent_id === root) { r.push(o[a.id]); } else { o[a.parent_id] = o[a.parent_id] || {}; o[a.parent_id].children = o[a.parent_id].children || []; o[a.parent_id].children.push(o[a.id]); } }); return r; }(data, null), // null is the root value of parent_id sorted = tree.reduce(function traverse(r, a) { return r.concat(a.data, (a.children || []).reduce(traverse, [])); }, []) console.log(sorted); console.log(tree); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

我更改了您的數據集以使示例更易於理解。 我使用了幾個函數來對數據進行排序:

 let arr = [ {country: 'USA', state:'Washington', city:'Washington DC'}, {country: 'USA', state:'Washington', city:'Boston'}, {country: 'USA', state:'California', city:'San Diego'}, {country: 'Brazil', state:'North', city:'C'}, {country: 'Brazil', state:'North', city:'B'}, {country: 'Brazil', state:'South', city:'A'}, {country: 'Turkey', state:'East', city:'f'}, {country: 'Turkey', state:'East', city:'e'}, {country: 'Turkey', state:'West', city:'d'}, ]; let expected = [ {country: 'Brazil', state:'North', city:'B'}, {country: 'Brazil', state:'North', city:'C'}, {country: 'Brazil', state:'South', city:'A'}, {country: 'Turkey', state:'East', city:'e'}, {country: 'Turkey', state:'East', city:'f'}, {country: 'Turkey', state:'West', city:'d'}, {country: 'USA', state:'California', city:'San Diego'}, {country: 'USA', state:'Washington', city:'Boston'}, {country: 'USA', state:'Washington', city:'Washington DC'}, ]; const sortByCountry = arr => { return arr.sort((city1,city2) => city1.country > city2.country); }; const sortByState = arr => arr.sort( (city1,city2) => { // Don't compare when countries differ. if (city1.country !== city2.country) return 0; return city1.state > city2.state; } ); const sortByCity = arr => arr.sort( (city1,city2) => { // Don't compare when countries or states differ. if (city1.country !== city2.country) return 0; if (city1.state !== city2.state) return 0; return city1.city > city2.city; } ); let result = sortByCity(sortByState(sortByCountry(arr))); console.log('------expected-----'); console.log(expected); console.log('------result-----'); console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM