Hey everyone, so I was wondering what kind of naming systems do people use for multiple objects in a folder in cases where you need it to be in order. Example, when looping through parts in a specific order.
Naming things for example “Part1” “Part2” etc. Doesn’t keep things in alphabetical order past 10.
One idea I came up with is this.
So A = 0 then you go A1 A2 A3 etc. Until B which is 10 then B1 B2 B3.
It works up until Z9 which is 169. Which is good for most use cases.
Just wondering is there any coding standards to achieve numbering in alphabetical order that are better than my method or more easily readable?
If you want to sort or loop through an array of elements that have a certain naming scheme in order, like Part1, Part2 … Part23, Part24, etc., then you can use the table.sort() function.
You can use string.match to grab the number out of the names of the instances and then just compare the values.
So if you had a folder containing a bunch of parts named in a particular way and wanted to sort them in order, you can do it like this:
local array = {...} -- just imagine this contains a bunch of parts with some naming scheme like 'Part1' and they are not ordered
table.sort(array, function(a, b)
local numA = tonumber(string.match(a.Name, "%d+"))
local numB = tonumber(string.match(b.Name, "%d+"))
return numA < numB
end)
-- array is now sorted :)
Ill definetly test this out. One question thought. The parameters a, b what are those for, just the names? I wouldnt have to add a new parameter for every name would I? It just cause i notice the table is already being passed as array. So im just not sure what the parameters are for
table.sort() requires an array to be sorted and a callback function that will be passed two arguments. a and b are just the names I gave for the parameters. a and b are going to be two values contained in the table to be compared against.
If your table contained a bunch of parts, then a and b will represent some random parts in the table that need to be compared against each other. It will sort through all the elements in the table, so you don’t need a loop. table.sort() does it all for you.
So i could easily make an automatic naming and arranging system with this, like when an object is added name it with a value tostring then use this to rearrange them in order on the table. Genius. This will save me hours of renaming. Lol, I was trying to get a rhythm going with the keyboard shortcuts to rename things faster.