Skip to content

Instantly share code, notes, and snippets.

@howmanysmall
Created August 6, 2020 16:28
Show Gist options
  • Save howmanysmall/a29a0b979d163842ce5e3e22a6193350 to your computer and use it in GitHub Desktop.
Save howmanysmall/a29a0b979d163842ce5e3e22a6193350 to your computer and use it in GitHub Desktop.
local Functions = {}
local function Alphabetically(a, b)
local typeA = type(a)
local typeB = type(b)
if typeA == typeB then
if typeA == "number" then
return a < b
else
return string.lower(tostring(a)) < string.lower(tostring(b))
end
else
return typeA < typeB
end
end
local function AlphabeticalOrder(Dictionary)
local Count = 0
local Order = {}
for Key in next, Dictionary do
Count += 1
Order[Count] = Key
end
table.sort(Order, Alphabetically)
local Index = 0
return function(Table)
Index += 1
local Key = Order[Index]
return Key, Table[Key], Index
end, Dictionary, nil
end
local function AlphabeticalOrderCoroutine(Dictionary)
local Count = 0
local Order = {}
for Key in next, Dictionary do
Count += 1
Order[Count] = Key
end
table.sort(Order, Alphabetically)
local Index = 0
return coroutine.wrap(function()
Index += 1
local Key = Order[Index]
while Key do
coroutine.yield(Key, Dictionary[Key], Index)
Index += 1
Key = Order[Index]
end
end)
end
local Dictionary = {
Hello = "World";
Goodbye = "Universe";
Boat = "Bomber";
Crazy = "man32";
HowMany = "Small";
Pobam = "L+";
}
Functions["regular function"] = function()
for Key, Value in AlphabeticalOrder(Dictionary) do
end
end
Functions["coroutine"] = function()
for Key, Value in AlphabeticalOrderCoroutine(Dictionary) do
end
end
require(4185109675).new(1, "Title", Functions)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment