Is there any way to sort a ZO_Tree? (I can't find one)
If not anyone have any ideas on how to get this added to my tree and keep it sorted. I'm out of ideas. The only thing I can think of is to take everything out of my organized table structure, dump all of the information into separate numerical tables, and then use ipairs(..)
I have a lot of information sorted in tables like directories. Each table may contain data and/or more tables, like this:
Warning: Spoiler
Lua Code:
table1 ={
aaaa ={
aaaa ="stuff",
bbbb ="stuff",
}
bbbb ={
aaaa ="stuff",
bbbbb ="stuff",
ccccc ={
aaaa ="stuff",
bbbb ="stuff",
}
}
ccccc ="stuff",
dddd ="stuff",
eeee ={
aaa ="stuff",
bbb ="stuff",
}
ffff ="stuff",
exc...
}
This is what I'm doing right now. It works & keeps the tree "organized" (but not sorted) like my tables, the pairs(..) function messes up the sort order.
Lua Code:
local count =0
localfunction AddItems(tTable, parentNode)
for key, value inpairs(tTable)do
count = count + 1
-- For testing purposes, stop after 1500 entries
if count >1500thenbreakend
iftype(value)=="table"then
local nextParentNode = self.navigationTree:AddNode("Header", {["text"]=key}, parentNode, nil)
AddItems(value, nextParentNode)
else
local childNode = self.navigationTree:AddNode("NavigationEntry", {["text"]=key, ["value"]=value}, parentNode, nil)
Yeah, to elaborate on Garkin's post: Lua makes 0 guarantees about the order for pairs(). You'd need to use ipairs() if you need a specific ordering, which means an array.
I'm was just hoping I wouldn't have to copy it into a numerical table & resort it. I had already tried it and it takes about 4 seconds to resort & populate the tree. It has about 3500 entries in it. I guess I need to find a way to restructure the tables.
Do you really need 3500 top level entries in your tree? I don't know what exactly you plan to do, but having such a large amount of information in one level does not sound very useful to me.
edit: okay. I think I see your problem now after rereading your code.
edit2: so after reading through ZO_Tree I think it should be as easy as adding
to the setupFunction of your template.
That way it will only sort the currently visible part of the tree when it is refreshed, which should be exactly what you want?
Last edited by sirinsidiator : 03/15/15 at 05:24 AM.
Do you really need 3500 top level entries in your tree? I don't know what exactly you plan to do, but having such a large amount of information in one level does not sound very useful to me.
edit: okay. I think I see your problem now after rereading your code.
edit2: so after reading through ZO_Tree I think it should be as easy as adding
to the setupFunction of your template.
That way it will only sort the currently visible part of the tree when it is refreshed, which should be exactly what you want?
Oh, I meant total entries. Yes, that is exactly what I wanted. I just tried again, leaving the rest of the code as-is & just adding a sort to reverse the sort order, but I still couldn't get it to work. It does sort the child table, but the items still displayed in the order they were added. Calling RefreshVisible() on the node didn't help, neither did calling Commit() on the tree again.
I also tried sorting right after I finished adding the items to the parent node, before commit ever gets called, but same results. The child tables show that they have been sorted, their order in the table changes, but it was still displaying them in the order they were added.
Found the problem. Inside AddChild it anchors the children and it will never update them afterwards.
In order to show the new order of your children array you need to do something like this after you sorted it: