How do i sort all contents of a table by alphebatecal order

how do i do that because i dont know

Use table.sort

local exampleTable = {"banana", "apple", "orange", "pear", "grape"}

table.sort(exampleTable)

for _, fruit in exampleTable do
    print(fruit)
end

Output:

apple
banana
grape
orange
pear
2 Likes

attempt to compare Instance < Instance
Is the erorr i get

1 Like

Sorting an array of instances, you need to compare the name

So do this instead

table.sort(exampleTable, function(a.Name, b.Name)
    return a < b
end)
1 Like

i dont understand what you mean

1 Like

Your table has instances, if we are trying to sort them by alphabetical order, we need to compare their names. Thats how the table.sort function works. If you need extra help, send me the table you are working with.

local exampleTable = workspace:GetChildren()

table.sort(exampleTable, function(a0: Instance, a1: Instance)
	return a0.Name < a1.Name
end)

print(exampleTable)

You can read here to understand more:

1 Like

at this point im gonan ask someone else to make this script i dont know anything about this

1 Like

I think what @yieldlua said is about as easy as it gets! 5 lines of code is pretty cool when you consider how complicated a problem this is. Thankfully, many giants of computer programming have already done this stuff (and done it efficiently). @yieldlua presented it as simple as it can be…

1 Like