Why does sorting my table give me back "nil"?

I’m trying to sort my values 0 - 3 via a table so it’s easier to get a different amount of them every time. But when I try to sort them I get “nil” back? I don’t understand what’s wrong. Everything works but when I print “sortedpages” I get “nil”.

script is cut off a bit

	for i,v in ipairs(pageinf:GetChildren()) do
		if v:IsA("StringValue") then
			table.insert(pages, v.Name)
		end
	end
	
	local sortedpages = table.sort(pages, function(A, B)
		return table.find(pages, A) < table.find(pages, B)
	end)
	
	print(sortedpages)

image

When printing the pages normally they are never sorted. I can give more info if needed.

table.sort doesnt return a table, it sorts the original table that you’ve used as an argument

1 Like

but when I try to get the original table it is not sorted correctly (or at least in the way i expect)?

image

You’re trying to sort the indexes, which are automatically already least to greatest. Use A < B instead.

what do you mean “Use A < B instead.”? I am already using that or am I misunderstanding something?

Make sure that what you’re passing through are proper number datatypes when you’re trying to loop through your pageinf Instance

From what I see, you’re obtaining the Names & inserting that inside the table, but they aren’t converted to a number which doesn’t change anything about the table unless if you convert them to number datatypes

You have to convert the names into numbers by using the tonumber() method, then sort the table from there

    for i,v in ipairs(pageinf:GetChildren()) do
		if v:IsA("StringValue") then
            local NumberConversion = tonumber(v.Name)

            if NumberConversion ~= nil then
			    table.insert(pages, NumberConversion)
            end
		end
	end
	
    table.sort(pages, function(A, B)
        return A < B
    end)
	
	print(pages)
2 Likes

ah that makes more sense, I don’t know why I thought it would automatically be numbers for some reason.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.