How do i sort this table?

I want to sort this table

local a = {
     ["firstvalue"] = {
          3
     },
      ["secondvalue"] = {
          2
     },
      ["thirdvalue"] = {
          1
     },
}

to

local a = {
     ["thirdvalue"] = {
          1
     },
      ["secondvalue"] = {
          2
     },
      ["firstvalue"] = {
          3
     },
}

how to script??

1 Like

Can you use table.sort()

1 Like

There isnā€™t really an automatic way to do it because Roblox doesnā€™t know what ā€œfirstā€, ā€œsecondā€ or ā€œthirdā€ means. Itā€™s just a random string of text that humans are able to read.

You can do it with tables and string manipulation though. It isnā€™t as easy as table.sort as you are using nested tables but hereā€™s one way to go about it:

local suffixes = { -- We need to write these out manually
    [1] = 'first';
    [2] = 'second';
    [3] = 'third';
    [4] = 'fourth';
    [5] = 'fifth';
    [6] = 'sixth';
    [7] = 'seventh';
    [8] = 'eighth';
    [9] = 'nineth';
    [10] = 'tenth';
}
local numbersToSort = {} -- create a temporary folder for numbers to sort
for i,v in pairs(a) do
    numbersToSort[#numbersToSort + 1] = (suffixes[v[1]]) -- insert the number to save for after
end

a = {} -- we want to overwrite the table to a new one.
for i,v in pairs(numbersToSort) do
    a[suffixes[i]..'value'] = {i} -- create the new entry in the a table
end

print(a)

--[[ Should output:
    { ['firstvalue'] = {1};
      ['secondvalue'] = {2};
      ['thirdvalue'] = {3} }
]]
1 Like

You can use table.sort like this:

table.sort(a, function(valueA, valueB)
    return valueA[1] < valueB[1]
end)

Read this article for more information about table and table.sort: table | Roblox Creator Documentation. Also, it does not make sense to only store one value in a table. Perhaps you can rewrite your table like this:

local a = {
     firstvalue = 3,
     secondvalue = 2,
     thirdvalue = 1,
}
1 Like

The issue is that itā€™s a dictionary, not an array, so you canā€™t sort it that easily

1 Like

I just tested my code and it seemed to work.

I do not understand why. For as far as I know

valueA and valueB are the values of the indices. This is my test by the way:

local a = {
     ["firstvalue"] = {
          3
     },
      ["secondvalue"] = {
          2
     },
      ["thirdvalue"] = {
          1
     },
}

table.sort(a, function(valueA, valueB)
    return valueA[1] < valueB[1]
end)

for k, v in pairs(a) do
    print(k, v)
end

The test code provided just prints keys in undefined order and the address of the table.
table.sort only sort indexes that are integers: https://www.lua.org/source/5.1/ltablib.c.html#auxsort. Most methods in table only accounts for integer indexes.

1 Like

We need more context. Why do you want to sort that specific table and not a simpler one like

local a = {1, 2, 3, 4, 5}

ā€¦?

My test fails when you flip ā€˜<ā€™ to ā€˜>ā€™. This question seems to be surprisingly hard lol.

I will add value in table more when i use

Yea but still you can just do

local table1 = {
[1] = {Name = "Something"}, 
[2] = {Name = "Amazing"}, 
}

Why are you even setting up your code in this way? What are you using it for? Can you not use

local a = {
     [3] = "firstvalue",
     [2] = "secondvalue",
     [1] = "thirdvalue",
}

and then iterate through it in an ipairs loop?
Or iterate to 1 backwards from three and subtract four by the current i + 1?

for i = 3,1,-1 do
    local index = (4 - i)
    print(index)
end

or just iterate normally?

for i = 1,3 do
    print(i)
end


Anyways, now that Iā€™ve gotten my questioning out of the way, you are not able to sort dictionaries because they do not use the same indexing as arrays do. I recommend checking out this resource:

2 Likes

You would use table.sort to do this. But Iā€™m not sure if dictionaryā€™s work with that.

Hereā€™s an example code of using table.sort:

local scores = {
{
Score = 1000,
Name = 'Noob'
}

{
Score = 5,
Name = 'NewPlayer'
},

{
Score = 800,
Name = 'Pro'
},

{
Score = 60,
Name = 'Owner'
}
} 

table.sort(scores, function(a, b)
return a.Score > b.Score
end) 

print(scores)
1 Like

Dictionaries cannot be sorted.
Only arrays can be sorted.

1 Like

Wait, do you have to write out the [key] manually?

I do

local suffixes = {"first","second","third","fourth","fifth","sixth","seventh","eighth","ninth","tenth"}

No, you shouldnā€™t have to. Itā€™s just mainly for visuals.

Surprising how such a simple looking question turned out to beā€¦ somewhat impossible lol

Alright, I wracked my brain, and the solution is simple. You must iterate through it twice. Please understand however that this will not sort the dictionary but will give you the keys you want in order during iteration.

local function IterateWierdDict(dict)
    local i = 1
    for _,value in pairs(dict) do
        for k,v in pairs(dict) do
            if i >= 4 then
                break
            end
            if v[1] == i then
                i += 1

                -- Your code that you want to execute in order
            end
        end
    end
end

However, if this does not suit your fancy I recommend organizing your table in a simpler manner.