Problem with sorting numbers. (an attempt to compare the number < Instance)

Hello!
I have a folder and the folder contains 8 parts and each of them has two attributes: Busy (Bool value) and Index (Number value). I need to sort all indexes (numbers) in these parts, but at the same time check in each part the value == false. And then I need to sort all these indexes (numbers). I wrote a script for this, but where the table needs to be sorted, this error occurs: an attempt to compare the number < Instance. How to fix it? I will be very grateful if you help me!

Script:

local AllValues = Folder:GetChildren()
	for i, point in pairs(AllValues) do
	 if point:GetAttribute("Busy") == false then
	  if type(i) == "number" and i ~= nil then
	   print(i)
	     AllValues[i] = point:GetAttribute("number")
	   end
     end
end
table.sort(AllValues) ------an error occurs here
LowestValue = AllValues[1]
print("The smallest number: "..LowestValue)
1 Like

There are numbers and instances within your table so the sort Function isnt exactly sure what to do, so it just throws an error, if you want to make your own custom sorting function add a second parameter as a function and this function has two parameters, CurrentValue and NextValue, we’ll call them a and b, within the function you should return true If a < b

Example code:

local myTable = {32, 12, 94, 50, 78}

table.sort(myTable, function(a, b)
      return a > b -- sort from lowest to highest
end)

print(myTable)

Okay, I inserted it into my script, but for some reason he writes me the same error

local AllValues = Folder:GetChildren()
 for i, point in pairs(AllValues) do
  if point:GetAttribute("Busy") == false then
   if type(i) == "number" and i ~= nil then
    print(i)
    AllValues[i] = point:GetAttribute("number")
     end
end
end
	table.sort(AllValues, function(a, b)
	return a > b -- error
       end)
	print(AllValues)

Did you even bother to read what i said? Its just an example code

I read it carefully conducted a test, when I inserted nil into the table, everything worked as it should (he printed the table), and when I inserted true and text, he gave this error. Is this how it should be?

I finally solved this problem! Thanks

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