How to sort data

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
tomes
Posts: 31
Joined: Saturday 07 July 2018 22:08
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Poland
Contact:

How to sort data

Post 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
User avatar
waltervl
Posts: 5904
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: How to sort data

Post by waltervl »

You should be able to use the standard Lua table.sort() function: https://www.tutorialspoint.com/sort-fun ... rogramming#
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
tomes
Posts: 31
Joined: Saturday 07 July 2018 22:08
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Poland
Contact:

Re: How to sort data

Post 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
User avatar
waltervl
Posts: 5904
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: How to sort data

Post by waltervl »

I really do not know. You can give it a try. The link I gave had some nice examples.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest