I’m trying to sort a table that has tables inside it. The table structure looks like this:
[lua]T = {}
T[1] = {}
T[1].counter = 7, T[1].ID = 14, T[1].price = 200
T[2] = {}
T[2].counter = 2, T[2].ID = 9, T[2].price = 300
T[3] = {}
T[3].counter = 3, T[3].ID = 30, T[3].price = 19[/lua]
How can I sort T[1] through T[3] in order of the smallest ID, for instance? I’m kind of a novice at the nested thing.
[import]uid: 96383 topic_id: 29691 reply_id: 329691[/import]
Not the best way, but it should reorder.
[code]
T = {}
T[1] = {}
T[1].counter = 7, T[1].ID = 14, T[1].price = 200
T[2] = {}
T[2].counter = 2, T[2].ID = 9, T[2].price = 300
T[3] = {}
T[3].counter = 3, T[3].ID = 30, T[3].price = 19
local function sortID(t)
local highest = 0
– first find highest number
for i = 1, #t do
if t[i].ID > highest then
highest = t[i].ID
end
end
–reorder
local temp = {}
for i = 1, #t do
for c = 1, highest do
if t[i].ID == c then
temp[#temp+1] = t[i]
end
end
end
return temp
end
T = sortID(T)
[/code] [import]uid: 118379 topic_id: 29691 reply_id: 119195[/import]
Thanks, works great! For anyone reading this later, line #25 needs to read “for c = 0, highest do” [import]uid: 96383 topic_id: 29691 reply_id: 119294[/import]
It doesn’t work right for me unless I switch the looping logic. In addition, by looping in the reversed sequence, you will not miss including duplicates. My example sorts on price, where there is a duplicate.
[code]
T = {}
T[1] = {}
T[1].counter = 7; T[1].ID = 14; T[1].price = 200
T[2] = {}
T[2].counter = 2; T[2].ID = 9; T[2].price = 300
T[3] = {}
T[3].counter = 3; T[3].ID = 30; T[3].price = 19
T[4] = {}
T[4].counter = 4; T[4].ID = 55; T[4].price = 200
local function sortPrice(t)
local highest = 0
– first find highest price
for i = 1, #t do
if t[i].price > highest then
highest = t[i].price
end
end
–reorder
local temp = {}
for c = 0, highest do
for i = 1, #t do
if t[i].price == c then
temp[#temp+1] = t[i]
end
end
end
return temp
end
T = sortPrice(T)
[/code] [import]uid: 23636 topic_id: 29691 reply_id: 119440[/import]
Hey, should have come back to this - realized soon after I posted that it was working that it was not working
Here’s my solution, which is working for me:
[lua] function sortID(t)
local highest = 0
local temp = {}
x = 0
for c=1, #t do
for i =1, #t do
print ( "i = "…i )
if tonumber(t[i][1].purchaseID) > highest then
highest = tonumber(t[i][1].purchaseID)
v = i
end
end
temp[c] = t[v]
table.remove( t, v )
highest = 0
end
return temp
end
T = sortID(T)[/lua]
Includes some code specific to my implementation, but you get the idea.
[import]uid: 96383 topic_id: 29691 reply_id: 119445[/import]
Glad my example was close enough to get you to your destination. Couldn’t test it at time to see if bang on. [import]uid: 118379 topic_id: 29691 reply_id: 119497[/import]
It was very helpful and much appreciated. Sure am fond of this community. Thanks again. [import]uid: 96383 topic_id: 29691 reply_id: 119519[/import]
It was very helpful and much appreciated. Sure am fond of this community. Thanks again. [import]uid: 96383 topic_id: 29691 reply_id: 119519[/import]