Page 5 of 5

Re: Mini Functions Repository

Posted: Sat Sep 10, 2016 6:41 pm
by Positive07
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?

Re: Mini Functions Repository

Posted: Sat Sep 10, 2016 8:12 pm
by HugoBDesigner
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)

Re: Mini Functions Repository

Posted: Sun Sep 11, 2016 1:12 am
by Positive07
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)

Re: Mini Functions Repository

Posted: Sun Sep 11, 2016 1:20 am
by davisdude
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 '>'!

Re: Mini Functions Repository

Posted: Sun Sep 11, 2016 2:51 am
by Positive07
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)

Re: Mini Functions Repository

Posted: Sun Sep 11, 2016 3:58 am
by zorg
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

Re: Mini Functions Repository

Posted: Sun Sep 11, 2016 4:24 am
by Positive07
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...