Mrdmlxo
(Mrdmlxo)
August 2, 2022, 11:51am
#1
---------------Pls off darkmode----------------
local tableToSort = {
{“Player1”, 2};
{“Player2”, 1};
{“Player3”, 3};
}
local sortedTable = table.sort(tableToSort,
function(a,b)
return a[2] > b[2]
end
)
for i,v in pairs(sortedTable) do
print(v[1]…" | "…v[2])
end
In simple, I’m looking to sort a table by an integer (highest to lowest) contained within the arrays of a table.
I had a go below, but couldn’t quite get it to work:
local tableToSort = {
{"Player1", 2};
{"Player2", 1};
{"Player3", 3};
}
local sortedTable = table.sort(tableToSort,
function(a,b)
return a[2] > b[2]
end
)
for i,v in pairs(sortedTable) do
print(v[1].." | "..v[2])
end
Desired output:
Player 3 | 3
Player 1 | 2
Player 2 | 1
Here’s a basic table.sort script used to set ranks some values.
And it works good so I cited this shape to my script.
Here’s what I made. Only one different thing is just values in first(in red square).
Then let’s add print and see the result of this script
Nah, it became idiot. Why it doesnt rank the values from the bigger suddenly?
Somebody help me
p.s - the values what I set are variable.
2 Likes
Well there is a simple answer, you forgot to set; mytab = the table.sort
, so your table.sort should look like:
mytab = table.sort(mytab, function(a,b)
return a[2] >= b[2]
end)
1 Like
Mrdmlxo
(Mrdmlxo)
August 2, 2022, 12:07pm
#3
mm I changed that too… but I changed because the script worked strangely when return a[2] > b[2].
First Ill do it again like what you advise
You could keep it as >=
I didn’t realize I forgot the =
Mrdmlxo
(Mrdmlxo)
August 2, 2022, 12:14pm
#5
that’s ok
But I found an error message in it
The 31 line in rauecaranked is this. What does message mean?
I have a table sorting function, and for some reason, i was getting the same issue as you, however i found when i named the things in my table, and then check everything else nearly the same, just fixed it.
example:
local itemtable = {
{["playername"] = "player1", ["Number"] = 5};
{["playername"] = "player2", ["Number"] = 2};
{["playername"] = "player3", ["Number"] = 15};
}
table.sort(itemtable, function(a, b)
return a.Number> b.Number -- instead of doing a[2] > b[2] i instead did a.Number > b.Number
end)
Mrdmlxo
(Mrdmlxo)
August 2, 2022, 12:29pm
#7
nooooooo
Here what I changed. Is it right?
: original
Mrdmlxo
(Mrdmlxo)
August 2, 2022, 1:09pm
#8
With this error message, print result started not to appear suddenly
this is line 25.
Remove the mytab
redeclaration, table.sort
doesn’t return anything
1 Like
McThor2
(McThor2)
August 2, 2022, 1:17pm
#10
What is the classname for: game.Workspace["miridal3:"].Value
McThor2
(McThor2)
August 2, 2022, 2:03pm
#14
That isn’t the class name, what type of object value is it? StringValue, numberValue etc
Mrdmlxo
(Mrdmlxo)
August 2, 2022, 2:08pm
#15
string but it’s not the value to be compared. It shows the username in ‘mirdal3:’.
Mrdmlxo
(Mrdmlxo)
August 2, 2022, 2:09pm
#16
Now error message died! THX1!!
But bug still alive
Here’s whole editted script. dam what should I do
McThor2
(McThor2)
August 2, 2022, 2:22pm
#17
So is the game.Workspace["miridal3:"].IHHI
one a string value too?
McThor2
(McThor2)
August 2, 2022, 2:23pm
#18
I think you may just need to wrap your values with tonumber
like tonumber(workspace["mirdal3:"].IHHI.Value)
1 Like
Mrdmlxo
(Mrdmlxo)
August 2, 2022, 3:03pm
#19
it works TANKYOU THANKYOU TAAAAAAAAAAAANKYOU!!!
I did all the occasion, That All ACCASION WORKED welL! HAHA
Forummer
(Forummer)
August 2, 2022, 3:21pm
#20
Stop using StringValue objects to store numbers. Just use IntValue/NumberValue objects instead.