Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I get a table to be sorted the way it is as the code says?

Asked by 4 years ago

Hello, I'm making a shop system that places itself onto a gui but its not listed as it should be seen on the "ShopList" table. For example it prints out Shotgun, Pistol and then Raygun where it should actually say Pistol, Shotgun and then Raygun. I don't know any solutions..

local ShopList = {
    ['Pistol'] = {Image = "rbxassestid://00000"},
    ['Shotgun'] = { Image = "rbxassestid://00000"},
    ['Raygun'] = {Image = "rbxassestid://00000"},

}

for Table,Info in pairs(ShopList) do
    print(Table)
    print('Loaded '.. Table..' Shop item')
end

0
There seems to be a print function, what's outputted? ninja_eaglegamer 61 — 4y
0
It shows as this "Shotgun, Raygun, Pistol" UltraUnitMode 419 — 4y

1 answer

Log in to vote
1
Answered by 4 years ago

Dictionaries cannot be organized in a certain order. If you want it to do that you could either create a table in which you print it in the order you'd like:

local shopList = {
    ['g1'] = '12344'
    ['g2'] = '44321'
}

local order = {'g1', 'g2'}
for i,v in pairs(order) do
    print(shopList[v])
end

or you can not use table.sort, except you'd have to format your table differently.

local shopList = {
    {'g1', 'asset/1234', 1}
    {'g2', 'asset/4321', 2}
}

table.sort(shopList, function(a,b)
    return a[3] > b[3]
end)

for i,v in pairs(shopList) do
    local itemName = v[1]
    local assetId = v[2]
    local order = v[3]
end
Ad

Answer this question