Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

(SOLVED)table.sort keeps giving me an output of nil, Why?

Asked by
IDKBlox 349 Moderation Voter
4 years ago
Edited 4 years ago

i'm trying to use table.sort() but it's not seeming to work out for me.

Any idea what i'm doing wrong?

local list = {1,66,45,76542,3,7,324,56,8,98,46,73757,34,12,5,1,2,3,4,6,7,9,33,55,67,14}

local sorted = table.sort(list)

print(sorted)

Output:

nil

0
you cant print a table like that you will have to loop through the table using a for loop. Fad99 286 — 4y
0
not true, if you print a table like that it'll print the table. https://gyazo.com/8211dc3acdb32cf1df6a4c63d24b4ec5. In the gyazo I pint(list) in the provided code above. IDKBlox 349 — 4y

1 answer

Log in to vote
0
Answered by
IDKBlox 349 Moderation Voter
4 years ago

I would just delete the entire question because I figured it out and it was super simple... and I figure if I had this problem maybe someone else will or has as well.. So Here is the solution

table.sort() NEVER RETURNS ANYTHING so....

local list = {1,64,3,6,865,80,6,245,8,3456,997,97,0,75,3}
local a = table.sort(list)
print(a)

Output:

nil

a will ALWAYS = nil a will NEVER not = nil

Therefore... SOLUTION

local list = {1,64,3,6,865,80,6,245,8,3456,997,97,0,75,3}

table.sort(list)

print(table.concat(list, "|"))

Output:

0|1|3|3|6|6|8|64|75|80|97|245|865|997|3456

Conclusion: table.sort() sorts that list given. It does NOT create a new 'sorted' list.

I'm aware I should have known this. But I overlooked things. Hope my issue helps someone lol

Ad

Answer this question