java - Algorithm for sorting a "plain-list-tree-structure" -
sorry subject, didn't find better title :-)
i have tree structure, here's "node" class:
public class categoria implements serializable { private static final long serialversionuid = 1l; @id @generatedvalue(strategy = generationtype.identity) private long id; @notnull private string name; @onetomany(cascade=cascadetype.all,fetch=fetchtype.eager) @joincolumn(name = "parent_id") private list<categoria> children = new linkedlist<categoria>(); @manytoone(fetch=fetchtype.lazy) @joincolumn( name = "parent_id", insertable=false, updatable=false ) private categoria parent; @transient private integer depth; private integer ordernumber; ... getters, setters, .... } don't care hibernate/jpa annotations, there's no problem them, think of ideal pojo world.
i made recursive method builds "plain" list of adjacent nodes. so, imagine tree:
grandfather |_ father |_ son1 |_ son2 |_ uncle grandmother |_ mother we have resulting list 1 (number "depth"): - grandfather (1) - father (2) - son1 (3) - son2 (3) - uncle (2) - grandmother (1) - mother (2)
all works good.
now want let users edit nodes sorting (between same depth nodes), mean: if want "son2" before "son1" in above list?
so tought add "ordernumber" property: ordernumbers 0 initially. user set son1's ordernumber 99 , son2's ordernumber 88.
the question is: how can rearrange resulting list sorting based on ordernumber?
but wait.... want sort "sublists", sons sorting absolutely not related "fathers" , "uncles" sorting!
thank helping us.
edit: you're missing 1 thing. did not explain myself well. here's example:
- grandfather (depth:1, ordernumber:1)
- father (depth:2, ordernumber:1)
- son1 (depth:3, ordernumber:1)
- son2 (depth:3, ordernumber:2)
- uncle (depth:2, ordernumber:2)
- grandmother (depth:1, ordernumber:2)
- mother (depth:2, ordernumber:1)
now want swap son1 , son2 resulting list be:
- grandfather (depth:1, ordernumber:1)
- father (depth:2, ordernumber:1)
- son2 (depth:3, ordernumber:1)
- son1 (depth:3, ordernumber:2)
- uncle (depth:2, ordernumber:2)
- grandmother (depth:1, ordernumber:2)
- mother (depth:2, ordernumber:1)
how can implement sort / compareto such purpose??
let categoria implement comparable. create custom compareto implementation, order depth , on ties additional ordernumber attribute decides. work in sortable collection.
but depending on problem solving tree-structure may more suitable implement custom iterator tree instead of recursively creating "list snapshots" ?
Comments
Post a Comment