Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Sort two values
Started by andavidosp Sep 19 2018 07:49 PM

- - - - -
3 replies to this topic
lua language corona sort

Best Answer XeduR @Spyric , 20 September 2018 - 06:20 AM

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.

[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

andavidosp

[GLOBAL: userInfoPane.html]
andavidosp
  • Observer

  • 19 posts
  • Corona SDK

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 )  if (b.state == "finished") and (a.state ~= "finished") then
       return b.stage < a.stage
  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!



[TOPIC: post.html]
#2

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Contributor

  • 616 posts
  • Corona SDK

  Best Answer

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.



[TOPIC: post.html]
#3

andavidosp

[GLOBAL: userInfoPane.html]
andavidosp
  • Observer

  • 19 posts
  • Corona SDK

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


[TOPIC: post.html]
#4

andavidosp

[GLOBAL: userInfoPane.html]
andavidosp
  • Observer

  • 19 posts
  • Corona SDK

Finally i resolved like you said me  :D




[topic_controls]
[/topic_controls]