lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


You can pass a function to `table.sort` along with the table to be sorted, which returns a boolean value, that will be used when sorting the table. For example:

table.sort(vAdressen, function(adr1, adr2) adr1.Name > adr2.Name end)

You can use < to sort in the other direction as well, and change Name out for City if you want to sort by city instead.

On Wed, Oct 19, 2016 at 9:54 AM Dietmar Segbert <didi.segbert@arcor.de> wrote:
Hello,

i am new to lua. My Milestone 312 ACE, a Daisy-Player, Mp3-Player,
Audiorecorder, from Bones
 http://www.bones.ch

becomes in the next Firmware a lua-interface.

I can test the beta of that fw and so i want to program a little
adressbook.

Now i have a question to table.sort()

i have a list

local vAdressen = {}
local vAdr = {}
local vAdr.Name = ""
local vAdr.City = ""
...

I read the fields for the vAdr from a csv-file and store them  in

vAdressen[1] = vAdr
...
vAdressen[n] = vAdr

so that i have all my adress-datasets in vAdressen

Now  i want to sort the table vAdressen for name odr city or ...

If i do

print(vAdressen[1].Name]

the name of my first dataset will be printed

What st the correct syntax for the table.sort() function to sort for name
or city?

Thanks for your help.

Best regards

Dietmar Segbert


--