How to disable the table sort by alphabet?

I created a table where I placed the car ranks. From best to worst they look like S,A,B,C,D,F. But when I request a module with ranks, the system sorts this table alphabetically and it turns out A, B, C, D, F, S, which I don’t need. How can I disable this sorting?
How table looks in script

local module = {
	["S"] = Color3.new(1, 0.839216, 0),
	["A"] = Color3.new(1, 0.121569, 0),
	["B"] = Color3.new(0.439216, 0, 1),
	["C"] = Color3.new(0, 0.4, 1),
	['D'] = Color3.new(0, 1, 0.278431),
	["F"] = Color3.new(1, 0.478431, 0)
}

return module

What i get in print

{
                    ["A"] = 1, 0.121569, 0,
                    ["B"] = 0.439216, 0, 1,
                    ["C"] = 0, 0.4, 1,
                    ["D"] = 0, 1, 0.278431,
                    ["F"] = 1, 0.478431, 0,
                    ["S"] = 1, 0.839216, 0
                 }  -  Server - Game:4

1 Like

I suggest you use numbers internally for this kind of problem and display the ranks using their respective identifiers externally (example: 1 = S).

1 Like

I know that this is a way out, but I want to find a fix with letters. If there is no such thing, I will correct it with numbers

2 Likes

I am not an expert but i will say what i believe is true
When a table is created using strings for the keys. Roblox doesn’t keep track of which key is first, second, etc. Could you perhaps describe why you would need them in a specific order? as by thinking myself i cant figure out why.
Incase you don’t wanna tell me that, here is a possible workaround

local Table1 = require(script.ModuleScript)
local Table2 = {"S", "A", "B", "C", "D", "F"}

local DataForletterA = Table1[Table2[2]]
--This will return the data in Table1 for the second letter in Table2 (A)
--Or you could just consider doing
local DataForletterA = Table1["A"]

You should never rely on a table to retain the same order. As @Ask_Allow said, use numbers from 0 to 5 as keys, and add a field like DisplayRank to values. Example:

local module = {
        [0] = { Color = Color3.new(1, 0.839216, 0), DisplayRank = "S" },
       	[1] = { Color = Color3.new(1, 0.121569, 0), DisplayRank = "A" },
        --etc..
}

return module
2 Likes

slight correction:
“a table” → “a dictionary”

dictionaries are randomly ordered in vanilla lua, same thing applies here

I have cars sorted by rank from best to worst

please read the post before commenting, the first phrase states that “I created a table where I placed the car ranks. From best to worst they look like S,A,B,C,D,F.” which should have given you an idea as to why they wanted to keep the dictionary in a specific order.


Like this

I did read and i understand that. But i wanted to know in what way they wanted to use it, since i saw no reasonable use of having it in the correct order. I have no idea what you mean by “i didn’t read the post”

1 Like

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