Help with sorting tables

I am trying to sort through a table so i can easily pick out the lowest numbers. I’ve looked at some of the table methods on the Roblox’s documentation but cant find anything to help. the table.sort method doesn’t seem to do anything so I’ve tried scripting my own sorting function but it is having weird results.

local mytable = {}
--adding random values to 'mytable' with the index being a number which i want to sort by ie i want 10 to become mytable[1], 12 to become mytable[2] etc.
mytable[10] = 10
mytable[15] = 15
mytable[25] = {10,15,15}
mytable[12] = 12
mytable[17] = 17
mytable[23] = {12,17,23}

--Sorting function seemingly makes it worse, indicated by the prints
function SetNewTable(Table)
	
	local ReturnTable = {}
	
	for Index, V in pairs(Table) do
		table.insert(ReturnTable,V)
	end
	
	return ReturnTable
end

print(mytable) -- print my table before applying sort function
mytable = SetNewTable(mytable) -- apply sort function
print(mytable) -- print new 'sorted' table

The output is throwing me off because it seems to be better before my function but i don’t understand why that is the case.

image

Any help in understanding how tables work in this case would be greatly appreciated. Thanks!

2 Likes

so your trying to sort a table by the value from least to greatest? but why would you have more tables as values? its not really possible to sort without knowing if {10, 15, 15} is greator then or less than 17

1 Like

If I’m understanding your issue correctly, you need to insert your keys into the ReturnTable array, sort them, then replace them with their original values in the Table table, like this:

local mytable = {}
--adding random values to 'mytable' with the index being a number which i want to sort by ie i want 10 to become mytable[1], 12 to become mytable[2] etc.
mytable[10] = 10
mytable[15] = 15
mytable[25] = {10,15,15}
mytable[12] = 12
mytable[17] = 17
mytable[23] = {12,17,23}

--Sorting function seemingly makes it worse, indicated by the prints
function SetNewTable(Table)

	local ReturnTable = {}
	local WriteIndex = 0
	
	--Move keys into the array first.
	for Key in pairs(Table) do
		WriteIndex += 1
		ReturnTable[WriteIndex] = Key
	end
	
	--Sort the array of keys.
	table.sort(ReturnTable)
	
	--Replace the keys with their values in the original table.
	for Index, Key in pairs(ReturnTable) do
		ReturnTable[Index] = Table[Key]
	end

	return ReturnTable
end

print(mytable) -- print my table before applying sort function
mytable = SetNewTable(mytable) -- apply sort function
print(mytable) -- print new 'sorted' table
1 Like

Im trying to sort through the table from lowest to highest key, regardless of the value, in my actual case the value will be a Vector3 or an array of Vector3’s.

The key will be the ‘cost’ as a numerical value to get to that Vector3.

Therefore i want to be able to quickly find the lowest cost, to see what position or array of positions are available at the lowest cost.

The way i imagined quickly finding the lowest cost would be to sort the table from lowest to highest key, where the lowest key would then become index 1, second lowest 2 etc. so i could quickly find the lowest with mytable[1]

Should be ipairs for sequential sorting

Why hadn’t i thought of that lol. Thanks i was scratching my head for a while last night!

1 Like

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