How to check if text is sorted from A-Z?

Hii, I have this minigame where the player has to sort files from A-Z, but I’m not sure how to go about scripting it.


image

repeat task.wait() until game:IsLoaded()

local RS = game:GetService("ReplicatedStorage")
local filetask = RS.FileTask
local player = game.Players.LocalPlayer
local filed = script.Parent.Files

local FileNames = {}

local PossibleNames = {
	"Joy",
	"Love",
	"Peace",
	"Serenity",
	"Mind",
	"BrainTech",
	"Aid",
	"Anatomy",
	"Happiness",
	"Identity",
	"Radio",
	"Music",
}

function checktext()
	--check if folder1-5 are in ABC order
end

for i, button in ipairs(filed:GetChildren()) do
	button.MouseButton1Click:Connect(function()
		if button.Name ~= "Folder1" then
			local folder1text = filed.Folder1.TextLabel.Text
			local folder2text = filed.Folder2.TextLabel.Text
			local folder3text = filed.Folder3.TextLabel.Text
			local folder4text = filed.Folder4.TextLabel.Text
			local folder5text = filed.Folder5.TextLabel.Text
			--rearrange folder text, button pressed goes to the top, the rest shift down
			checktext()
		end
	end)
end

function generatefilenames()
	while 5 do
		local randomentry = PossibleNames[math.random(1, #PossibleNames)]
		--check if randomentry doesnt equal anything in FileNames
		table.insert(FileNames, randomentry)
		
	end
end

filetask.OnClientEvent:Connect(function(desk)
	script.Parent.Enabled = true
	
	
end)

The comments written are where I don’t know what to do

local function compare(t1: {string}, t2: {string}): boolean
	if #t1 ~= #t2 then return false end
	for i, v in pairs(t1) do
		if v ~= t2[i] then return false end
	end
	return true
end

local function areSorted(names: {string}): boolean
	local clone = table.clone(names)
	table.sort(clone) --this assumes ascending alphabetic order
	return compare(names, clone)
end

local names = {"Joy", "Love", "Peace"} --etc

if areSorted(names) then
	print("The names are sorted!")
else
	print("The names aren't sorted!")
end

You also need another function for fetching the values of each TextLabel and putting them in the names array when you need to check the order.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.