I want to be able to take an array and sort it alphabetically based on the Name property of the objects inside.
For example, if we have an input of:
{dog, cat, hamster, turtle, cat}
then output should be:
{cat, cat, dog, hamster, turtle}
(every variable contains a Name (i.e dog.Name = “Dog”, cat.Name = “cat”, etc), and it sorts alphabetically based on that)
Note that the same value can appear more than once, like in the example above where "cat" appears twice.
I’ve actually already found a “solution” but… it is incredibly hacky.
Current Solution:
local items = localItemInventory:GetChildren()
local alphabeticallySortedItems = {}
local alphabeticallySortedKeys = {}
for i, item in ipairs(items) do
local updatedItemName = item.Name
local loopCount = 0
while table.find(alphabeticallySortedKeys, updatedItemName) do
loopCount += 1
updatedItemName = item.Name .. tostring(loopCount)
end
table.insert(alphabeticallySortedKeys, updatedItemName)
end
table.sort(alphabeticallySortedKeys)
for i, item in ipairs(items) do
for i, v in ipairs(alphabeticallySortedKeys) do
if item.Name == string.sub(v, 1, string.len(item.Name)) then
alphabeticallySortedItems[i] = item
alphabeticallySortedKeys[i] = "ABCDEFGHJILKMNAOP"
break
end
end
end
Any better solutions or methods would be appreciate