how to sort tables?

Moderator: leecollings

Post Reply
User avatar
emme
Posts: 909
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi / ODroid
Domoticz version: latest
Location: Milano, Italy
Contact:

how to sort tables?

Post by emme »

Ciao,

is there a way to obtain (or generate new one) the otherdevices table sorted somehow?
by name would be the best, but even by IDX would be nice...

actually if I browse the table on the same script, I get different order everytime.... (_lastupdate? )
for my use... it is actually impossible to handle an unsorted table :P

ciao
M

P.S.
as explainantion this is a simple browsing of the table:

Code: Select all

commandArray = {}

print ('-----------------------------------')

for devName, devStatus in pairs(otherdevices) do
    if (string.sub(devName,1,4)) == 'MAG_' then 
        print ('>> TEST >>> '..devName)
    end 
end 
print ('-----------------------------------')

return commandArray
2017-03-10 15:08:40.213 LUA: -----------------------------------
2017-03-10 15:08:40.213 LUA: >> TEST >>> MAG_Bagnetto_Finestra
2017-03-10 15:08:40.213 LUA: >> TEST >>> MAG_Cucina
2017-03-10 15:08:40.213 LUA: >> TEST >>> MAG_Cameretta
2017-03-10 15:08:40.214 LUA: >> TEST >>> MAG_Bagnetto
2017-03-10 15:08:40.214 LUA: >> TEST >>> MAG_Camera
2017-03-10 15:08:40.214 LUA: >> TEST >>> MAG_Ingresso
2017-03-10 15:08:40.214 LUA: -----------------------------------
2017-03-10 15:08:40.346 LUA: -----------------------------------
2017-03-10 15:08:40.346 LUA: >> TEST >>> MAG_Ingresso
2017-03-10 15:08:40.346 LUA: >> TEST >>> MAG_Bagnetto
2017-03-10 15:08:40.346 LUA: >> TEST >>> MAG_Camera
2017-03-10 15:08:40.346 LUA: >> TEST >>> MAG_Bagnetto_Finestra
2017-03-10 15:08:40.346 LUA: >> TEST >>> MAG_Cameretta
2017-03-10 15:08:40.346 LUA: >> TEST >>> MAG_Cucina
2017-03-10 15:08:40.346 LUA: -----------------------------------
the
table.sort(otherdevices)
seems not to be a solution....
The most dangerous phrase in any language is:
"We always done this way"
User avatar
darlomrh
Posts: 35
Joined: Monday 23 May 2016 8:03
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: UK
Contact:

Re: how to sort tables?

Post by darlomrh »

Looking at https://www.lua.org/pil/19.3.html arrays have no order and you have to force a key order.

using the examples on the page would this suffice?

Code: Select all


function pairsByKeys (t, f)
    local a = {}
    for n in pairs(t) do table.insert(a, n) end
    table.sort(a, f)
    local i = 0      -- iterator variable
    local iter = function ()   -- iterator function
        i = i + 1
        if a[i] == nil then return nil
        else return a[i], t[a[i]]
        end
    end
    return iter
end


for devName, devStatus in pairsByKeys(otherdevices) do
    print ('>> TEST >>> '..devName)
end



User avatar
emme
Posts: 909
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi / ODroid
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: how to sort tables?

Post by emme »

thanks for your reply!! :P

I found that example too, but it was unsucessful... so I dig my way out of this :P

I created this function:

Code: Select all

-- ++++++++++++++++++++++++++++++++++++++++++++
-- ++ Create new sorted by name Device Table ++
-- ++++++++++++++++++++++++++++++++++++++++++++
function sortOtherdevices(devType)
    newTable = {}

    for devName, devStaus in pairs(otherdevices) do 
        if string.sub(devName,1,4) == devType then 
            table.insert(newTable, (devName..','..devStaus))
        end 
    end 
    
    table.sort(newTable)
    return newTable 
end 
this is how I call the table in other part of the script

Code: Select all

local newTable = {}
   newTable = sortOtherdevices('XXXX')
   for newIDX, newLine in pairs(newTable) do 
      devName, devStatus = newLine:match("([^,]+),([^,]+)")
the problem I discovered is that PROBABLY Lua has a table (string) limit in passed arguments... the entire otherdevices table won't get through
so I decided to limit only to the devices I would need (they all have the same first 4 characters)
in this way the table works
:P
The most dangerous phrase in any language is:
"We always done this way"
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest