Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why is this not sorting the table?

lost = {1,6,0,8,2,5}

table.sort(lost, function(a, b)
   return lost[a] < lost[b]
end)

for i, v in pairs(lost) do
   print(i, v)
end

1 answer

✓ Accepted answer

You have interpreted wrong how table.sort works. The values passed to the functions are not the indices, but in fact, the values! (That is, lost[a] will be a, and lost[b] is b).

What you mean is:

lost = {1,6,0,8,2,5}

table.sort(lost, function(a, b)
   return a < b
end)

for i, v in pairs(lost) do
   print(i, v)
end

1 comment

1

THANK YOU !

This discussion is preserved as it was. New answers, comments, and votes are closed.