Hey, and thanks for reading in advance.
In the options menu of a fighting game I’m helping to code, there’s a window where you can play any song used in the various stages and modes. I want to alphabetize the songs by name, so I came up with a lengthy sort function - I just wanted to see if anyone had a shorter or more optimal method.
Code:
local titles = {"Avalanche", "Bob-omb Battlefield", "C.R.O.W.N.E.D.", "Death by Glamour"}
function alphabetize(list)
local alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
local letters = {}
for letter in alphabet:gmatch("%a") do
letters[#letters+1] = letter
end
table.sort(list, function(a, b)
local priorityA, priorityB = 0, 0
for _,char in a:gmatch("%a") do
for i,letter in ipairs(letters) do
if char == letter then
priorityA = priorityA + i
break
end
end
end
for _,char in b:gmatch("%a") do
for i,letter in ipairs(letters) do
if char == letter then
priorityB = priorityB + i
break
end
end
end
return priorityA < priorityB
end)
return list
end
for _,title in pairs(alphabetize(titles)) do
print(title)
end