Mini Functions Repository

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
Positive07
Party member
Posts: 1005
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: Mini Functions Repository

Post by Positive07 » Sat Sep 10, 2016 6:41 pm

HugoBDesigner wrote:I wrote a simple array organizer for numerical entries. I know these things come with Lua already, but after I wrote one for strings (long ago, dunno where to find it), I figured I'd give numbers a try:
Mmm sorry but wouldn't [manual]table.sort[/manual] work?
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(Github.com/Positive07)

User avatar
HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Somewhere in Brazil
Contact:

Re: Mini Functions Repository

Post by HugoBDesigner » Sat Sep 10, 2016 8:12 pm

Yeah, it would, but it was mostly experimental. I already knew of table.sort, but figured I'd give it a try to see how it'd work.
Speaking of that, I finally found the alphabetical organizer: it was in my Minigame Simulator all along :D

Code: Select all

function alphabetic_order(a, b)
	local g = "abcdefghijklmnopqrstuvwxyz"
	local alphabet = {}
	for i = 1, string.len(g) do table.insert(alphabet, string.sub(g, i, i)) end
	
	local a, b = string.lower(a), string.lower(b)
	for j = 1, math.min(string.len(a), string.len(b)) do
		local aa, bb = string.sub(a, j, j), string.sub(b, j, j)
		if aa ~= bb then
			if table.contains(alphabet, aa) and table.contains(alphabet, bb) then
				if table.contains(alphabet, aa) < table.contains(alphabet, bb) then
					return true
				elseif table.contains(alphabet, aa) > table.contains(alphabet, bb) then
					return false
				end
			elseif table.contains(alphabet, aa) then
				return false
			else
				return true
			end
		elseif j == math.min(string.len(a), string.len(b)) then --virtually similar, return the smallest as first
			return (string.len(a) < string.len(b) and true) or false
		end
	end
end
And to use it:

Code: Select all

table.sort(table, function(a, b)
	return alphabetic_order(table[a], table[b])
end)
HugoBDesigner - my Twitter
HugoBDesigner - my Blog

User avatar
Positive07
Party member
Posts: 1005
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: Mini Functions Repository

Post by Positive07 » Sun Sep 11, 2016 1:12 am

You can take this part out of the function since it is constant:

Code: Select all

   local g = "abcdefghijklmnopqrstuvwxyz"
   local alphabet = {}
   for i = 1, string.len(g) do table.insert(alphabet, string.sub(g, i, i)) end
Then again, you can compare two strings in Lua with > and < and they sort alphabetically, not sure about caps but you can always do

Code: Select all

table.sort(t, function (a, b)
	return string.lower(a) < string.lower(b)
end)
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(Github.com/Positive07)

davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Mini Functions Repository

Post by davisdude » Sun Sep 11, 2016 1:20 am

How 'bout this? :cool:

Code: Select all

function alphabetic_sort(a,b)
    -- Handle words that are the same until a certain point
    local i = 1
    while a:sub( i, i ) == b:sub( i, i ) do
        i = i + 1
        -- No letter goes before words with letters
        if i > #a then return true end
        if i > #b then return false end
    end
    return a:sub( i, i ) < b:sub( i, i )
end
Use it in the same way:

Code: Select all

table.sort( tab, alphabetic_sort )
-- Reverse alphabetized
table.sort( tab, function( a, b ) return not alphabetic_sort( a, b ) end )
Plus, it doesn't rely on table.contains

*edit: Thanks to Positive07 for teaching me you can compare strings with '<' and '>'!
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim

User avatar
Positive07
Party member
Posts: 1005
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: Mini Functions Repository

Post by Positive07 » Sun Sep 11, 2016 2:51 am

I'm pretty sure you can do that just with table.sort:

Code: Select all

local t = {
    "something",
    "somewhere",
    "someone",
    "someday",
    "some",
    "so",
    "s",
    "abc"
}

table.sort(t)

for i, v in ipairs(t) do
   print(i, v)
end

--Output:
--1	abc
--2	s
--3	so
--4	some
--5	someday
--6	someone
--7	something
--8	somewhere
Note, uppercase are sorted first and then lower case, that is "S" is lower than "s" so if you need to ignore case then use the code I posted before:

Code: Select all

table.sort(t, function (a,b) return string.lower(a) < string.lower(b) end)
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(Github.com/Positive07)

User avatar
zorg
Party member
Posts: 2745
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Mini Functions Repository

Post by zorg » Sun Sep 11, 2016 3:58 am

String sorting probably follows the underlying encoding's numerical values, and since the first 127 should be somewhat standard across all "ansi" codepages (or, if you want me to be utterly explicit, will be equivalent to ISO/IEC_8859-1 and/or Windows-1252), the order of 0-9A-Za-z will be adhered to. Don't quote me on this though. :3
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.

User avatar
Positive07
Party member
Posts: 1005
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: Mini Functions Repository

Post by Positive07 » Sun Sep 11, 2016 4:24 am

I quote you on that, that is how it works, and it does it on a character by character basics, so if the first is equal it goes with the next one and so on...
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(Github.com/Positive07)

Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests