Pairs order problem / how to sort pairs order?

I made a table that contains default setting,
like this :

local DefaultSettings = {
	PC = {},
	Gamepad = {},
	Mobile = {},
	Others = {},
}

And i making SettingUI and trying to use loop for building some buttons.
like this :

local Order = 0
for key, value in pairs(DefaultSettings) do
	local Button = Instance.new("TextButton")
	Button.Name = key
	Button.Position = UDim2.new(0, Order*10, 0, 0)
	Order += 1
end

and the result of order is : [ Mobile :arrow_forward: PC :arrow_forward: Others :arrow_forward: Gamepad ].
as more easy to see :

local DefaultSettings = {
	PC = {},
	Gamepad = {},
	Mobile = {},
	Others = {},
}

for key, value in pairs(DefaultSettings) do
	print(key) -- Prints [ Mobile → PC → Others → Gamepad ]
end

print(DefaultSettings) --[[ Prints
{
	["Gamepad"] = {},
	["Mobile"] = {},
	["Others"] = {},
	["PC"] = {}
}
]]

I have no idea WHAT happend on my table order
Is there any way to sort pairs order?
I have tried table.sort but as the key is not number, it is not working.

I wanna keep my table variant and pairs loop script…

You could try numbering them,

local DefaultSettings = {
	1PC = {},
	2Gamepad = {},
	3Mobile = {},
	4Others = {},
}

Though this isn’t as good of a fix as I would like it to be

dictionaries aren’t meant to have any sort of order.
Arrays are structured like:
1 = One
2 = Two
3 = Three
And functions iterate by this index. Dictionaries instead of it use strings, and due to this it’s impossible to sort them in some way.

maybe this will help

local DefaultSettings = {
    PC = {
        Order = 1,
    },
    Gamepad = {
        Order = 2,
    },
    Mobile = {
        Order = 3,
    },
    Others = {
        Order = 4,
    },
}

i have no idea, maybe have a separate table of the strings and index the dictionary with that

Variant name cannot start with number.
So i tried [ aPC, bGamepad, cMobile, dOthers ].

result is same on pairs loop but print(DefaultSettings) prints solted table well

but i need sorted pairs

Not working.
even PC = {1}, Gamepad{2} not working too

What is the issue though i want to see the full script.

Also, do this print(value)

Not print(key)

There is also such thing as this

For key, value in pairs(DefaultSettings) do
Print(key)
Print(value)
End

Table.sort(DefaultSettings)

Also,i think u should read about tables on youtube and see how they do it

FOR Loop is like iterating from first to fourth .

You don’t need my full script.
just check this :

Original is atleast over 100 lines. you still need it?
Full Original script over 300 lines

I always think that full script is best.because ther are barely any errors in this.

ofcourse, as you said

local DefaultSettings = {
	[1] = {"PC",},
	[2] = {"Gamepad",},
	[3] = {"Mobile",},
	[4] = {"Others",},
}

for key, value in pairs(DefaultSettings) do
	print(value[1]) -- Prints [ PC → Gamepad → Mobile → Others ]
end

print(DefaultSettings) --[[Prints
{
	[1] = {"PC",},
	[2] = {"Gamepad",},
	[3] = {"Mobile",},
	[4] = {"Others",},
}
]]

Can be a solution. but no other way?
Do i MUST change my variable and whole functions?

You should read my question again

There is no other way for for loop.because it will insist printing from one to fourth.another way will be using math.random()

The PROBLEM is table is not SORTED
why you offering me math.random()

What u mean by table not sorted.what is your order of the table.

print(DefaultSettings) Should print :

{
	["PC"] = {},
	["Gamepad"] = {},
	["Mobile"] = {},
	["Other"] = {}
}

As order I created the array variables but it printing :

{
	["Gamepad"] = {},
	["Mobile"] = {},
	["Others"] = {},
	["PC"] = {}
}

U should do this then

Print(DefaultSettings(1))
And then so on

I hope this work because the default settings are in random order table.sort would work if u do this

Local defaultSettings = (

PC,
GAMEPAD,
TABLET,
PHONE.
)

I hope this will work for u

I don’t know of any other straight-forward way to create sorted dictionaries without storing the initial table in an array, since, according to the Roblox Creator Documentation site, dictionaries are not sorted when looping over it:

Unlike using ipairs() on an array, using pairs() on a dictionary doesn’t necessarily return items in the same order that they’re in the dictionary.


If you’d like to:

  • Retain the original format in some way (having the type of device act as the key and the table of settings as the value)

  • Make minimal changes to the existing code

  • Ensure it’s organized in the order that you intended

  • And improve its readability in the new format (without needing the device type as the first value for each of the settings tables)

then, you could create an additional table that has the sole purpose of defining which indexes correspond with which settings table:

local SettingsIndexes = {
	[1] = "PC",
	[2] = "Gamepad",
	[3] = "Mobile",
	[4] = "Others"
	
}

local DefaultSettings = {
	[1] = {},
	[2] = {},
	[3] = {},
	[4] = {}
}

---

local Order = 0
for index, value in DefaultSettings do
	local Button = Instance.new("TextButton")
	
	Button.Name = SettingsIndexes[index] --[[ With the current index of the
"DefaultSettings" table, it looks through the "SettingsIndexes" table,
which contains a string that corresponds with the name of the device type --]]
	
	Button.Position = UDim2.new(0, Order * 10, 0, 0)
	Order += 1
end