lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


There are also sorted pairs iterator code examples which could solve the problem maybe.

Le 29 juil. 2014 17:19, "Sean Conner" <sean@conman.org> a écrit :
It was thus said that the Great Benoit Germain once stated:
> Unfortunately I can't sort something like this (my data is more complex
> than that of course, I have 600 lines of simular Lua code per settings
> file):
>
>                 ["pushForceCurve"] =
>                 {
>                     ["param1"] = -4,
>                     ["param3"] = 0.502515,
>                     ["param2"] = 2
>                     ["centerOfMassOffset"] =
>                     {
>                        ["x"] = 0,
>                        ["y"] = 0
>                     },
>                 },

  Can you not change the serialization code to sort the keys if it's dumping
a table?  Something like:

        function serialize(name,item)
          ...

          if type(item) == 'table' then
            local keys = {}

            for name in pairs(items) do
              table.insert(keys,name)
            end

            -- -------------------------------------------------------------
            -- if all the keys are of a single type (strings, numbers) then
            -- this should be okay.  If the keys are of mixed types, then
            -- you will probably have to define a sort function that will
            -- reliably "sort" the mixed keys.
            -- -------------------------------------------------------------

            table.sort(keys)

            for _,name in ipairs(keys) do
              serialize(name,item[name])
            end
          end

          ...

        end

  -spc