|
Cheat Engine The Official Site of Cheat Engine
|
View previous topic :: View next topic |
Author |
Message |
Dr.Disrespect Grandmaster Cheater Reputation: 3
Joined: 17 Feb 2016 Posts: 526
|
Posted: Tue Mar 07, 2017 6:10 pm Post subject: Sorting a table by two columns? |
|
|
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.
Thanks.
_________________
**************
A simple example is better then ten links. |
|
Back to top |
|
|
panraven Grandmaster Cheater Reputation: 55
Joined: 01 Oct 2008 Posts: 942
|
Posted: Tue Mar 07, 2017 6:27 pm Post subject: |
|
|
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 |
|
|
Dr.Disrespect Grandmaster Cheater Reputation: 3
Joined: 17 Feb 2016 Posts: 526
|
Posted: Tue Mar 07, 2017 6:54 pm Post subject: |
|
|
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.
_________________
**************
A simple example is better then ten links. |
|
Back to top |
|
|
|
|
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
|
|