Sort Table Data

I have a key, value table and want to sort the table on a combination of alphabetic and age.

user\_list={ { id='1', name='John', age=11 }, { id='2', name='Zen', age=4 }, { id='1', name='John', age=3 }, { id='1', name='Av', age=7 }, { id='1', name='Av', age=2 } } local user\_sort=function( user1,user2 ) return user1.name \< user2.name end -- sort users alphabetically: table.sort(user\_list, user\_sort)

Output i want to achieve is:
 

Av 2

Av 7
John 3

John 11

Zen 4

The Above code sorts them alphabetically , but on every run the ages switch around.

How can i have them listed by name and age same time, without giving different result on each run?

The basic ‘compare’ function for table sorts follows this format:

local function compare( a, b ) return a \< b end&nbsp;

However, that won’t work for your table because it has a custom layout.  So try this instead:

local function compareByName( a, b ) return a.name:lower() \< b.name:lower() end local function compareByID( a, b ) return a.id \< b.id end local function compareByAge( a, b ) return a.age \< b.age end table.sort( user\_list, compareByName ) -- Or one of the others above

Ah, I just noticed, you want to sort by two parameters… give me a few minutes to consider this.

OK, try this:

user\_list={ { id='1', name='John', age=11 }, { id='2', name='Zen', age=4 }, { id='1', name='John', age=3 }, { id='1', name='Av', age=7 }, { id='1', name='Av', age=2 } } function sortByNameAndAge(s) -- convert hash to array local t = {} for k, v in pairs(s) do table.insert(t, v) end -- sort table.sort(t, function(a, b) if a.name ~= b.name then return a.name \< b.name end return a.age \< b.age end) return t end local sorted = sortByNameAndAge(user\_list) --print(sorted[1].name .. ": " .. sorted[1].players .. ", " .. sorted[1].visits) --print(sorted[2].name .. ": " .. sorted[2].players .. ", " .. sorted[2].visits) print("\nUnsorted\n------------------------------") for i = 1, #user\_list do print( user\_list[i].name, user\_list[i].age ) end print("\nSorted\n------------------------------") for i = 1, #sorted do print( sorted[i].name, sorted[i].age ) end

** NOTE ** This is from a solution that I then modified:

http://stackoverflow.com/questions/24894262/lua-sort-table-by-two-values

so thanks @romaninggamer, works great :slight_smile:

hum…why your user_list is in a table? it’s fixed values? if not, and your going to add more values to it, you will lose your new data when you close the app. Best approach (only if you need more values and need to access them later) is to build a small database, with a table and the fields you need…from there you can return the table with the order you want. 

That’s a different thing. Blablu asked for a way to sort table data. If you want to use it later, you might want to use a different approach, but that’s not the point of his question. 

Either way, you can perfectly use user_list as a table to make persistent data, just need to create the logic.

undecode, if you know anything about programming, you know that sometimes the problem is not in the answer but in the question…

if blablu1212 having problems sorting elements (thats the easiest thing we learn in programming classes) maybe the problems don’t stop there. i was trying to help to detect if the problem is only what he asked or more profund than that.

Yeah, you are 100% right. This is totally easier than printing “Hello World!”.

The basic ‘compare’ function for table sorts follows this format:

local function compare( a, b ) return a \< b end&nbsp;

However, that won’t work for your table because it has a custom layout.  So try this instead:

local function compareByName( a, b ) return a.name:lower() \< b.name:lower() end local function compareByID( a, b ) return a.id \< b.id end local function compareByAge( a, b ) return a.age \< b.age end table.sort( user\_list, compareByName ) -- Or one of the others above

Ah, I just noticed, you want to sort by two parameters… give me a few minutes to consider this.

OK, try this:

user\_list={ { id='1', name='John', age=11 }, { id='2', name='Zen', age=4 }, { id='1', name='John', age=3 }, { id='1', name='Av', age=7 }, { id='1', name='Av', age=2 } } function sortByNameAndAge(s) -- convert hash to array local t = {} for k, v in pairs(s) do table.insert(t, v) end -- sort table.sort(t, function(a, b) if a.name ~= b.name then return a.name \< b.name end return a.age \< b.age end) return t end local sorted = sortByNameAndAge(user\_list) --print(sorted[1].name .. ": " .. sorted[1].players .. ", " .. sorted[1].visits) --print(sorted[2].name .. ": " .. sorted[2].players .. ", " .. sorted[2].visits) print("\nUnsorted\n------------------------------") for i = 1, #user\_list do print( user\_list[i].name, user\_list[i].age ) end print("\nSorted\n------------------------------") for i = 1, #sorted do print( sorted[i].name, sorted[i].age ) end

** NOTE ** This is from a solution that I then modified:

http://stackoverflow.com/questions/24894262/lua-sort-table-by-two-values

so thanks @romaninggamer, works great :slight_smile:

hum…why your user_list is in a table? it’s fixed values? if not, and your going to add more values to it, you will lose your new data when you close the app. Best approach (only if you need more values and need to access them later) is to build a small database, with a table and the fields you need…from there you can return the table with the order you want. 

That’s a different thing. Blablu asked for a way to sort table data. If you want to use it later, you might want to use a different approach, but that’s not the point of his question. 

Either way, you can perfectly use user_list as a table to make persistent data, just need to create the logic.

undecode, if you know anything about programming, you know that sometimes the problem is not in the answer but in the question…

if blablu1212 having problems sorting elements (thats the easiest thing we learn in programming classes) maybe the problems don’t stop there. i was trying to help to detect if the problem is only what he asked or more profund than that.

Yeah, you are 100% right. This is totally easier than printing “Hello World!”.