Page 1 of 1

How to sort data

Posted: Sunday 10 July 2022 14:13
by tomes
Hi,
I have got variable "online":

Code: Select all

data = { online = { initial = {}, history = true, maxItems = 20 }}.
There are data like this (added by add command):
aaa, ccc, bbb, eee, ddd

Is it possible to sort it alphabetically or rewrite alphabetically to another data variable?
I will just add that aaa, bbb ..... are the names of Domoticz devices that are getting according to criteria determined by the command:

Code: Select all

domoticz.devices().forEach(function(device)
	if string.match(device.name, ' .* PINGER') ~= nil then
		domoticz.data.online.add(string.match(device.name, "(%w+) "))
        end
end)
And i want to print it out alphabetically .
I will be grateful for your help.

Tom

Re: How to sort data

Posted: Sunday 10 July 2022 20:46
by waltervl
You should be able to use the standard Lua table.sort() function: https://www.tutorialspoint.com/sort-fun ... rogramming#

Re: How to sort data

Posted: Sunday 10 July 2022 21:27
by tomes
Thank You! Is it possible to use it with
data = { online = { initial = {}, history = true, maxItems = 20 }}.

or should I convert to local variable and than use table.sort?
Tom

Re: How to sort data

Posted: Sunday 10 July 2022 22:51
by waltervl
I really do not know. You can give it a try. The link I gave had some nice examples.