How to sort an array alphabetically based on its values' property

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 :slight_smile:

This should do it!

local t = {'b', 'a', 'c'};

table.sort(t, function(a,b)
return a:lower() < b:lower();
end);

print(t[1], t[2], t[3]);
1 Like

This is correct, but I’d like to point out that semicolons aren’t required in Lua.

If they don’t effect the quality or functionality of the code, is there really an issue?
also I guess, you could say force of habit lol.

(also I don’t want to get off topic, so I’ll just leave it at that.)

If it’s a force of habit, it’s fine. I just wanted to point it out.

local function SortTable(array)
      table.sort(array)
      return array
end 

just do sort

@koyugaki is mostly correct but I think they may have misunderstood your question. You do need to use table.sort with a custom comparator:

table.sort(t, function(a, b) return a.Name < b.Name end)

You can add :lower() if you need.

1 Like

I really thought that wouldn’t work but I tried it and… it works… so well… thank you very much.

Unfortunately, I have no idea how that works at all… Could anyone explain what the second argument does?

its like looping trough the table and compare the values

for _,a in pairs(table) do
      for _,b in pairs(table) do
           if a:lower() < b:lower() then
                  --does the sorting
           end
     end
end

also you can just do table.sort(array)

1 Like

I managed to figure out how to make their solution work with specific properties, but thank you for that information nonetheless :slight_smile:

I think I kind of understand it now. Thank you, that information about it looping through the tables helped a lot