Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Sorting a table by two columns?

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
Dr.Disrespect
Grandmaster Cheater
Reputation: 3

Joined: 17 Feb 2016
Posts: 526

PostPosted: Tue Mar 07, 2017 6:10 pm    Post subject: Sorting a table by two columns? Reply with quote

Say, if I have a table like this:
Code:

table = {
         type= "work", id = 5}
         type= "game", id = 2}
         type= "work", id = 3}


I want to first sort the table based on the "type", and then sort it on "id" from the biggest to the smallest value. The result should be like this:
Code:

table = {
         type= "work", id = 5}
         type= "work", id = 3}
         type= "game", id = 2}


The only way I can think of is to sort the table based on "type" first, then separate the table into two tables based on their "type", then sort them based on "id", at last, combine them into the final table...... But there must be a smarter way to do this. Sad

Thanks.

_________________
**************

A simple example is better then ten links. Very Happy
Back to top
View user's profile Send private message
panraven
Grandmaster Cheater
Reputation: 54

Joined: 01 Oct 2008
Posts: 941

PostPosted: Tue Mar 07, 2017 6:27 pm    Post subject: Reply with quote

Try use a sort with comparison that the secondary keys of two items are checked only if the primary keys of the two items are the same,eg.
Code:

-- fix syntax: 'table' is a build-in module, should not assign as global variable
mytable = {
         {type= "work", id = 5} ,
         {type= "game", id = 2} ,
         {type= "work", id = 3} ,
}
table.sort(mytable, function(a,b)
  if  a.type == b.type then return a.id < b.id else return a.type < b.type end
end)

_________________
- Retarded.
Back to top
View user's profile Send private message
Dr.Disrespect
Grandmaster Cheater
Reputation: 3

Joined: 17 Feb 2016
Posts: 526

PostPosted: Tue Mar 07, 2017 6:54 pm    Post subject: Reply with quote

panraven wrote:
Try use a sort with comparison that the secondary keys of two items are checked only if the primary keys of the two items are the same,eg.
Code:

-- fix syntax: 'table' is a build-in module, should not assign as global variable
mytable = {
         {type= "work", id = 5} ,
         {type= "game", id = 2} ,
         {type= "work", id = 3} ,
}
table.sort(mytable, function(a,b)
  if  a.type == b.type then return a.id < b.id else return a.type < b.type end
end)


Thank you panraven, you helped me with sorting again. Very Happy

_________________
**************

A simple example is better then ten links. Very Happy
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites