organizing table variables in numerical order.

Say I wanted a table that took everyones scores and put them into a table, and then resorted them in that table so that they could be sorted in numerical order, rather than in order of implementation. Does anyone have a general concept on how I may do this?

@Paintcannon - in a situation like this, Google “lua sort”, and you will find lots of helpful web pages. If you are stuck on a language problem, ie how to do something with variables, tables etc, I would always try a search first, before posting on the forum.

When I started learning Lua, I read the whole manual and also found a nice summary of all the commands. That helps too.

Alright thanks for the help. I usually come here trying to learn something when I’m not sure what I’m looking for. I didn’t know that there was a table.sort predefined function. I was expecting a concept such as:

for a,o in pairs(scores) do
If o>scores[1] then
a=1
end
end

Not that specifically but something like that.

a and b represent the two values that are being compared. The function is use to sort the items if you want to do something special. If you just do table.sort( table), it just sorts the values in the table in numerical order. For a more in depth explanation do a Google search.

When I try that code (with the table having been defined) it gives me a technical error saying that n is a nil value

Well the idea of sorting tables was not specifically for sorting high scores, but I wanted to sort falling objects based on their height.

Can someone also explain how table.sort works exactly? what do a and b represent, why is there a function within the syntax?

@Paintcannon - try looking up or googling for a solution on how functions like sort work. This forum is for solving specific problems, not teaching you the Lua language.

The first parameter is the table you want to sort - I think it might need to use numerical indexes.

The second parameter is a function (or a reference to a function) that is called to compare two values (a & b) in the table. Behind the scenes the table.sort function calls the function you specify a load of times and each time it get’s called a & b represent two entries in the table. The function provides the “comparison” logic between the two elements so that you decide if “a” is less than (or greater than) “b”. If the return value is true then the elements a and b are swapped, otherwise they’re left alone. Table.sort then continues to do this until all the elements have been compared.

The thing to remember is that in Lua a table can contain anything (even another table) so there is no generic concept in Lua of being able to compare two table entries (other than to check if they both reference the same memory location), therefore it’s up to you as the developer to tell table.sort how to compare the elements.

But @Ignatz is right - you’d be better served looking up specific help on Lua if you need it - GIYF! :slight_smile:

Try something like this

tbl = {2, 1, 3}
table.sort(tbl, function(a, b) return a > b end)

(Untested)

if your scores are in column N of a table Scores, this should do it. note that the table must have numeric indexes

--sorts in descending order
table.sort( Scores, function(a,b) return a[N]>=b[N] end )