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
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