Help! Sort table alphabetically

Hello! I’m pretty new to Lua and using arrays in amy complexity (usually just a physics logic gal).

I have a table set up comprised of a list of beers, their country of origin, and their type, it looks like this:

[lua]

beerList = {

        {bName = “Foster’s Oil Can”, bCountry = “Australia”, bType = “Lager”},

        {bName = “Samichlaus”, bCountry = “Austra”, bType = “Lager”}

        …etc

}

[/lua]

There are about 200 beers on this list, and I need the user to be able to view them Alphabetically, Alphabetically by Country, or by Type (I assume I’ll also have it as alphabetically just to be easier).

I’ve been trying table.sort, but I’m assuming there’s a problem with it being a table and not a standard array. I also gave “table.sort(beerList.bName)” a shot, but it didn’t like that because it isn’t a table (“Table expected, got nil”).

How should I best proceed?

hi Jacqui,

from http://lua-users.org/wiki/TableLibraryTutorial

the table sort method has this signature

table.sort(table [, comp])

so, you give it the table to sort with an optional comparator function. in your situation it would be:

local sort\_func = function( a,b ) return a.bName \< b.bName end table.sort( beerList, sort\_func )

you can change the direction of the comparison to reverse the list ( ‘<’ to ‘>’ )

cheers,
dmc

Firstly, thank you so much for quick reply!

When I run that I get a Runtime error “attempt to compare string with nil, stack traceback: [C]: ?”

Do I need to start with a sample string name?

getting that error means there’s something wrong with your data – perhaps part of a record in the list is ‘nil’ (the bName or whatever field you’re comparing).

here is a full test for you:

beerList = { {bName = "Trumer Pils", bCountry = "Austria", bType = "Pilsner"}, {bName = "Samichlaus", bCountry = "Austria", bType = "Lager"}, {bName = "Foster's Oil Can", bCountry = "Australia", bType = "Lager"}, {bName = "Newcastle Ale", bCountry = "UK", bType = "Ale"}, } local sort\_func = function( a,b ) return a.bName \< b.bName end table.sort( beerList, sort\_func ) for i, record in ipairs( beerList ) do print(i, record.bName) end

output:

2013-06-28 12:30:25.079 Corona Simulator[16594:f03] 1 Foster's Oil Can 2013-06-28 12:30:25.080 Corona Simulator[16594:f03] 2 Newcastle Ale 2013-06-28 12:30:25.080 Corona Simulator[16594:f03] 3 Samichlaus 2013-06-28 12:30:25.080 Corona Simulator[16594:f03] 4 Trumer Pils

cheers,
dmc

I’ve been up and down the list repeatedly, and I can’t find a spot where there isn’t a value filled in. To see if I was missing something, I ran this code:

[lua]

    for iter = 1, #beerList do
        if beerList.bName[iter] == nil then
            print (“NotFound” )
        else
            print(beerList.bName[iter])
        end
    end

[/lua]

I hoped it would show me where the gaps were, but I’m getting the same error “attempt to index field ‘bName’ (a nil value)”. I get it twice, so I’m assuming there’s 2 blank spots. Where am I going wrong here?

that is a different error you’re getting. this latest one comes from your loop. the previous one was coming from the comparator function.

for the latest one, as your list is structured i would think the code should be like this:

for iter = 1, #beerList do if beerList[iter].bName == nil then print ("NotFound" ) else print(beerList[iter].bName) end end

if you don’t find the gaps after this modification, would you mind attaching the files as a zip so i can look at your code ? (under More Reply Options)

cheers,
dmc

Ah-ha! Found it! Thank you so much it was driving me crazy! <3 <3

glad i could help ! :slight_smile:

hi Jacqui,

from http://lua-users.org/wiki/TableLibraryTutorial

the table sort method has this signature

table.sort(table [, comp])

so, you give it the table to sort with an optional comparator function. in your situation it would be:

local sort\_func = function( a,b ) return a.bName \< b.bName end table.sort( beerList, sort\_func )

you can change the direction of the comparison to reverse the list ( ‘<’ to ‘>’ )

cheers,
dmc

Firstly, thank you so much for quick reply!

When I run that I get a Runtime error “attempt to compare string with nil, stack traceback: [C]: ?”

Do I need to start with a sample string name?

getting that error means there’s something wrong with your data – perhaps part of a record in the list is ‘nil’ (the bName or whatever field you’re comparing).

here is a full test for you:

beerList = { {bName = "Trumer Pils", bCountry = "Austria", bType = "Pilsner"}, {bName = "Samichlaus", bCountry = "Austria", bType = "Lager"}, {bName = "Foster's Oil Can", bCountry = "Australia", bType = "Lager"}, {bName = "Newcastle Ale", bCountry = "UK", bType = "Ale"}, } local sort\_func = function( a,b ) return a.bName \< b.bName end table.sort( beerList, sort\_func ) for i, record in ipairs( beerList ) do print(i, record.bName) end

output:

2013-06-28 12:30:25.079 Corona Simulator[16594:f03] 1 Foster's Oil Can 2013-06-28 12:30:25.080 Corona Simulator[16594:f03] 2 Newcastle Ale 2013-06-28 12:30:25.080 Corona Simulator[16594:f03] 3 Samichlaus 2013-06-28 12:30:25.080 Corona Simulator[16594:f03] 4 Trumer Pils

cheers,
dmc

I’ve been up and down the list repeatedly, and I can’t find a spot where there isn’t a value filled in. To see if I was missing something, I ran this code:

[lua]

    for iter = 1, #beerList do
        if beerList.bName[iter] == nil then
            print (“NotFound” )
        else
            print(beerList.bName[iter])
        end
    end

[/lua]

I hoped it would show me where the gaps were, but I’m getting the same error “attempt to index field ‘bName’ (a nil value)”. I get it twice, so I’m assuming there’s 2 blank spots. Where am I going wrong here?

that is a different error you’re getting. this latest one comes from your loop. the previous one was coming from the comparator function.

for the latest one, as your list is structured i would think the code should be like this:

for iter = 1, #beerList do if beerList[iter].bName == nil then print ("NotFound" ) else print(beerList[iter].bName) end end

if you don’t find the gaps after this modification, would you mind attaching the files as a zip so i can look at your code ? (under More Reply Options)

cheers,
dmc

Ah-ha! Found it! Thank you so much it was driving me crazy! <3 <3

glad i could help ! :slight_smile:

hooray for dmc! awesome dude xD

hooray for dmc! awesome dude xD