FusePowered

Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Help! Sort table alphabetically
Started by Jacqui.Zarka Jun 27 2013 04:19 PM

* * * - - 1 votes
8 replies to this topic
tables alphabetical sort arrays

Best Answer dmccuskey , 28 June 2013 - 12:48 PM

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

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

Jacqui.Zarka

[GLOBAL: userInfoPane.html]
Jacqui.Zarka
  • Starter
  • Pip
  • 9 posts
  • Newbie

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:

 

beerList = {
        {bName = "Foster's Oil Can", bCountry = "Australia", bType = "Lager"},
        {bName = "Samichlaus", bCountry = "Austra", bType = "Lager"}
        ...etc
}

 

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?

 

 



[TOPIC: post.html]
#2

dmccuskey

[GLOBAL: userInfoPane.html]
dmccuskey
  • Pro
  • PipPipPipPipPipPip
  • 143 posts
  • Jedi

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

[TOPIC: post.html]
#3

Jacqui.Zarka

[GLOBAL: userInfoPane.html]
Jacqui.Zarka
  • Starter
  • Pip
  • 9 posts
  • Newbie

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?



[TOPIC: post.html]
#4

dmccuskey

[GLOBAL: userInfoPane.html]
dmccuskey
  • Pro
  • PipPipPipPipPipPip
  • 143 posts
  • Jedi

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

[TOPIC: post.html]
#5

Jacqui.Zarka

[GLOBAL: userInfoPane.html]
Jacqui.Zarka
  • Starter
  • Pip
  • 9 posts
  • Newbie

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:

 

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

 

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?



[TOPIC: post.html]
#6

dmccuskey

[GLOBAL: userInfoPane.html]
dmccuskey
  • Pro
  • PipPipPipPipPipPip
  • 143 posts
  • Jedi

  Best Answer

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

[TOPIC: post.html]
#7

Jacqui.Zarka

[GLOBAL: userInfoPane.html]
Jacqui.Zarka
  • Starter
  • Pip
  • 9 posts
  • Newbie

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



[TOPIC: post.html]
#8

dmccuskey

[GLOBAL: userInfoPane.html]
dmccuskey
  • Pro
  • PipPipPipPipPipPip
  • 143 posts
  • Jedi

glad i could help ! :)

[TOPIC: post.html]
#9

reyanthonyrenacia

[GLOBAL: userInfoPane.html]
reyanthonyrenacia
  • Starter
  • PipPipPipPipPipPip
  • 118 posts
  • Jedi

hooray for dmc! awesome dude xD




[topic_controls]
[/topic_controls]



Also tagged with one or more of these keywords: tables, alphabetical, sort, arrays