table.sort (sorting with 2 criteria....)

Hello,

I have the following table :

local tblToSort = {}
tblToSort [1] = {name = “Orange”, showFirst = true)
tblToSort [2] = {name = “Apple”, showFirst = false)
tblToSort [3] = {name = “Pie”, showFirst = false)
tblToSort [4] = {name = “Milk”, showFirst = true)
tblToSort […] = …

I want to be able to show this table with all items that are showFirst == true in alphabetical order, then all item that are showFirst == false. Which mean the result would be the following:

Milk, Orange, Apple, Pie

I have try several permutation of table.sort to sort with those 2 parameters, but I am not successfull. Anybody would know the recipe for this?

thanks [import]uid: 28795 topic_id: 28160 reply_id: 328160[/import]

Hey there - try this (I can’t take all credit, Alex of Dunkel Games was very helpful);

[lua]local tblToSort = {}

tblToSort [1] = {name = “Orange”, showFirst = true}
tblToSort [2] = {name = “Apple”, showFirst = false}
tblToSort [3] = {name = “Pie”, showFirst = false}
tblToSort [4] = {name = “Milk”, showFirst = true}

local firstTable = {}
local secondTable = {}

for i = 1, #tblToSort do
if (tblToSort[i].showFirst) then
table.insert(firstTable, tblToSort[i].name)
else
table.insert(secondTable, tblToSort[i].name)
end
end

table.sort(firstTable)
table.sort(secondTable)

for i = 1, #firstTable do
print(firstTable[i])
end

for i = 1, #secondTable do
print(secondTable[i])
end[/lua]

Peach :slight_smile: [import]uid: 52491 topic_id: 28160 reply_id: 113796[/import]

Hi Peach,

Thanks for the sample, I will need to create another table to put back both sorted list, since I need one table to feed the list widget.

Nick

[import]uid: 28795 topic_id: 28160 reply_id: 113825[/import]