Sort two values

Hi,

 

I’m trying to sort a table like this:

 

local competitions =    { 

                {category = “Libertadores”, state = “finished”},

                {category = “Champions”, state = “timed”},

                {category = “Sudamericana”, state = “timed”},

                {category = “Libertadores”, state = “timed”}

 }

 

via :

local sort\_function = function( a,b )&nbsp; if (b.state == "finished") and (a.state ~= "finished") then &nbsp; &nbsp; &nbsp; &nbsp;return b.stage \< a.stage &nbsp; end end table.sort( competitions, sort\_function)

I would like to obtain this result:

 

{category = “Champions”, state = “timed”},

{category = “Sudamericana”, state = “timed”},

{category = “Libertadores”, state = “timed”},

{category = “Libertadores”, state = “finished”}

 

 But i am obtaining this:

 

{category = “Libertadores”, state = “timed”},

{category = “Sudamericana”, state = “timed”},

{category = “Champions”, state = “timed”},

{category = “Libertadores”, state = “finished”}

 

What I want is that when the state is “finished” I send the object to the end of the array but I leave the other positions in the order they were

 

Can anyone help me to solve this?

 

Thanks!

I did something similar to this some time ago by creating two temporary tables. You could do something like:

 

local tFinished = {} local tTimed = {} for i = 1, #competitions do if competitions[i].state == "timed" then tTimed[#tTimed+1] = {category=competitions[i].category,state="timed"} else tFinished[#tFinished+1] = {category=competitions[i].category,state="finished"} end end

Now that you have separate tables for timed and finished, you can sort them individually if needed. Then you just use another loop to add the tFinished entries to the end of tTimed table, or create an entire new table and place them all there.

Thanks i solved doing this:

for i = 1, #competitions do if (competitions[i].state == "FINISHED") then local array\_temporal = competitions[i] table.remove(competitions, i) table.insert( competitions, #competitions + 1, array\_temporal ) end end

Finally i resolved like you said me  :smiley:

I did something similar to this some time ago by creating two temporary tables. You could do something like:

 

local tFinished = {} local tTimed = {} for i = 1, #competitions do if competitions[i].state == "timed" then tTimed[#tTimed+1] = {category=competitions[i].category,state="timed"} else tFinished[#tFinished+1] = {category=competitions[i].category,state="finished"} end end

Now that you have separate tables for timed and finished, you can sort them individually if needed. Then you just use another loop to add the tFinished entries to the end of tTimed table, or create an entire new table and place them all there.

Thanks i solved doing this:

for i = 1, #competitions do if (competitions[i].state == "FINISHED") then local array\_temporal = competitions[i] table.remove(competitions, i) table.insert( competitions, #competitions + 1, array\_temporal ) end end

Finally i resolved like you said me  :smiley: