 |
Cheat Engine The Official Site of Cheat Engine
|
View previous topic :: View next topic |
Author |
Message |
sir-gunny Advanced Cheater
Reputation: 0
Joined: 15 Mar 2012 Posts: 81
|
Posted: Mon Apr 06, 2020 6:36 am Post subject: Sort a multidimensional table?? |
|
|
Hi.
How can i sort my table by name?
From:
Code: | local original_table = {
{12,"Mike Z."},
{86,"Xena K."},
{54,"Anton B. (2)"},
{58,"Anton B. (1)"},
{71,"Björn H."},
{22,"Luis M."}
} |
To:
Code: | local sorted_table = {
{58,"Anton B. (1)"},
{54,"Anton B. (2)"},
{71,"Björn H."},
{22,"Luis M."}
{12,"Mike Z."},
{86,"Xena K."}
}
|
|
|
Back to top |
|
 |
Dark Byte Site Admin
Reputation: 470
Joined: 09 May 2003 Posts: 25796 Location: The netherlands
|
Posted: Mon Apr 06, 2020 6:47 am Post subject: |
|
|
Code: |
table.sort(t, function(a,b)
return a[2]<b[2]
end)
|
_________________
Do not ask me about online cheats. I don't know any and wont help finding them.
Like my help? Join me on Patreon so i can keep helping |
|
Back to top |
|
 |
sir-gunny Advanced Cheater
Reputation: 0
Joined: 15 Mar 2012 Posts: 81
|
Posted: Mon Apr 06, 2020 7:01 am Post subject: |
|
|
Ah, i couldn't see the wood for the trees^^
THX
|
|
Back to top |
|
 |
sir-gunny Advanced Cheater
Reputation: 0
Joined: 15 Mar 2012 Posts: 81
|
Posted: Tue Feb 22, 2022 11:41 am Post subject: |
|
|
And how can i sort this:
Code: | tMyTable = {
{"D",2},
{"B",1},
{"D",1},
{"B",2},
{"C",2},
{"C",1},
{"A",2},
{"A",1}
}
table.sort(tMyTable, function(a,b)
return a[1]<b[1] and a[2]<b[2]
end)
for i=1,#tMyTable do
print(tMyTable[i][1],tMyTable[i][2])
end |
to this:
Code: |
A 1
B 1
C 1
D 1
A 2
B 2
C 2
D 2 |
I just can't get this to work... Sorry
|
|
Back to top |
|
 |
Dark Byte Site Admin
Reputation: 470
Joined: 09 May 2003 Posts: 25796 Location: The netherlands
|
Posted: Tue Feb 22, 2022 2:10 pm Post subject: |
|
|
Code: |
tMyTable = {
{"D",2},
{"B",1},
{"D",1},
{"B",2},
{"C",2},
{"C",1},
{"A",2},
{"A",1}
}
table.sort(tMyTable, function(a,b)
if a[2]~=b[2] then
return a[2]<b[2]
else
return a[1]<b[1]
end
end)
for i=1,#tMyTable do
print(tMyTable[i][1],tMyTable[i][2])
end
|
_________________
Do not ask me about online cheats. I don't know any and wont help finding them.
Like my help? Join me on Patreon so i can keep helping |
|
Back to top |
|
 |
sir-gunny Advanced Cheater
Reputation: 0
Joined: 15 Mar 2012 Posts: 81
|
Posted: Wed Feb 23, 2022 5:21 am Post subject: |
|
|
Thanks.
|
|
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
|
|