How can I sort my table in order?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I have an table of cutscene parts and each one is called, 1,2,3,etc. in order they go, I am trying to get a table of them all and sort them so I can loop threw them and tween them to each, how do I do this?
  2. What is the issue? Include screenshots / videos if possible!
    Everything I have tried doesn’t work.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Yes, I couldn’t figure out how to use table.sort for it so would be nice for someone to explain it :slight_smile:

you use table.sort() I think you pass an array and it sorts the elements in number order (if they are numbers) or you can pass a function and sort it. Idk I never used it or just sort the manually by iterating through them.

They are not numbers, it is a getchildren() but the parts named in numbers so how can I sort the names?

table.sort() passes two arguments to the function you provide, if you did. I’ll call these arguments a and b. If the function returns true, element a will go before element b in the table. So you can do something like this;

function sort(a, b)
    return tonumber(a.Name) < tonumber(b.Name);
end
local tableToSort = {};
table.sort(tableToSort, sort);

If you want to to sort from greatest to least, you can use a > instead of a <.

4 Likes