Extended table sorting in lua

I am trying to sort an expanded table but am not successful.

This is how my table structure looks like:

{
    ["12345"] = {12345, "Something", {"Stuff"}},
    ["523544"] = {523544, "Something", {"Stuff"}},
    ["6744"] = {6744, "Something", {"Stuff"}},
    ["146"] = {146, "Something", {"Stuff"}},
    ["724572"] = {724572, "Something", {"Stuff"}},
    ["54"] = {54, "Something", {"Stuff"}},
    ["146"] = {146, "Something", {"Stuff"}},
    ["146"] = {146, "Something", {"Stuff"}},
    ["12345"] = {12345, "Something", {"Stuff"}},
    ["44"] = {44, "Something", {"Stuff"}},
}

      

and I would like to sort it from largest to smallest:

{
    ["724572"] = {724572, "Something", {"Stuff"}},
    ["523544"] = {523544, "Something", {"Stuff"}},
    ["12345"] = {12345, "Something", {"Stuff"}},
    ["12345"] = {12345, "Something", {"Stuff"}},
    ["6744"] = {6744, "Something", {"Stuff"}},
    ["146"] = {146, "Something", {"Stuff"}},
    ["146"] = {146, "Something", {"Stuff"}},
    ["146"] = {146, "Something", {"Stuff"}},
    ["54"] = {54, "Something", {"Stuff"}},
    ["44"] = {44, "Something", {"Stuff"}},
}

      

I ran into several problems here.

  • It cannot store 2 numbers of equal value
  • I cannot sort it properly from largest to smallest

As for why the indexes are strings, if I do table[623258195] = "Example"

, the table will create indexes 623258195 causing my program to crash.

For why values ​​are tables, it stores other important information which is the second and third values ​​in the table, with the first being the numeric form of the index.

Hopefully I'm clearing up and I'm sorry if this is considered a duplicate question, I didn't find anything in the last hour of searching that helped me.

+3


source to share


2 answers


  • You will need to modify the data structure to support multiple values ​​using the same id / key:

    {
        [12345] = {
            {12345, "foo", {"bar"}}, -- You'll probably want to sort these somehow.
            {12345, "baz", {"qux"}}
        },
        [123] = {
            {123, "foo", {"bar"}}
        }
    }
    
          

  • You can use table.sort(tbl, f)

    together with an index table:

    local unsorted = {} -- Data, in your format, inside this table.
    local index = {} -- Table which will contain sorted keys (then you loop over this, get unsorted[k])
    for k in pairs(unsorted) do
        index[#index+1] = k -- Populate the keys to sort.
    end
    table.sort(index, function(a, b) 
     return b < a -- Order highest to lowest, instead of lowest - highest (default)
    end)
    
          



Here's the complete code sample and results. http://ideone.com/thE1zP

+1


source


You cannot "sort" the keys in a hash, since they have no "order" in the way that a whole sequence of keys in a table does.

You can change the data structure to turn the hash into a table, but an easier way would be to have a separate table with only hash keys and sort their values; when you need to get the elements from the hash in a specific order, you just loop through the (sorted) elements in that table and then retrieve the elements from the hash.

In any case, you won't be able to store multiple values ​​for the same key you are trying to do:



{
...
    ["146"] = {146, "Something", {"Stuff"}},
    ["146"] = {146, "Something", {"Stuff"}},
    ["146"] = {146, "Something", {"Stuff"}},
...
}

      

You need to store them in a table (and refer to that table with a key "146"

) or rethink why you need different item keys for the same key value.

0


source







All Articles