Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How would i sort my table `upside down` in this case?

Hello, i am making data saving system and before replicating the data, i am storing them into a table to prevent unnecessary replications.

So here is my table:

{
values = {},
collections = {},
folders = {},
}

I want to parent values first, then collections and at the end i want to parent folders BUT when i am printing the order to the output i always get:

folders table: 0xf7c6601989c35ea5
collections table: 0xb3067eb76aa08325
values table: 0x51a475714b125865

That's the exact opposite from what i need. Here is what i need:

values table: 0x51a475714b125865
collections table: 0xb3067eb76aa08325
folders table: 0xf7c6601989c35ea5

I was searching and found something like table.sort even tho i don't know how to use it in this case. I also had an idea of doing:

local index = #self
for 1, #self in pairs(self) do
	-- do stuff
	index = index - 1
end

Even tho i would like a better way to do it if it's possible.

Thanks, kirda

2 comments

0

well answer to your first problem: do print(game:GetService("HttpService"):JSONEncode(values)) and it will print. a null means it's an instance inside of the table.

0

nono there is not problem with the table, all the values inside are tables, there is no instances, my problem is that it does not sort it by the order i need it to. 

1 answer

✓ Accepted answer

Unfortunately there's not really an easy way of doing this. This will might be fine for what you want. HOWEVER READ BELOW

Got a part of this function from stackoverflow

function reverseTable(orgTable)
	local i=0
	local tempTable = {}
	for _,entry in pairs(orgTable) do  --sanitize the #table format: Ex: t={} ; t[5]="BAD" ; print(#t) --will be 0 
		i += 1
		tempTable[i] = entry
	end
	local reverseOrgTable = {}
	for i=1, #tempTable do
		reverseOrgTable[i] = tempTable[#tempTable + 1 - i]
	end
	return reverseOrgTable
end

Dictionary tables are NOT GUARANTEED to be in any particular order. ie: using strings as keys

so you could do

myTable = {
	{ values = {} },
	{ collections = {} },
	{ folders = {} },
}

to access stuff in folders:

myTable[1]["folders"][key or numb here] --outputs something in the folder table

Also you might be able to use the fact that tables are really just pointers to a spot in memory. Watch this tomfoolery:

folder = {}
myTable = {}
myTable[1] = folder

folder[5] ="Hello"
print(myTable[1][5]) --prints Hello

This discussion is preserved as it was. New answers, comments, and votes are closed.