+ Reply to Thread
Results 1 to 9 of 9

Thread: Sorting Routing

  1. #1
    Telaran
    Join Date
    Oct 2011
    Posts
    86

    Default Sorting Routine

    Could someone point me to a sorting routine that would work on a nested table.
    I know how to sort a table such as table[#].name but looking for something that is table[#][#].name
    Last edited by TinnerKB; 08-11-2013 at 03:07 PM.

  2. #2
    Shield of Telara Adelea's Avatar
    Join Date
    Mar 2011
    Posts
    734

    Default

    Something more than?

    Code:
    for k,v in pairs(table) do
    	table.sort(v, function(a,b) return a.name < b.name end)
    end
    Which would basically sort each "sub table" within the table.

    If this isnt what you meant, then some more details with example data would be helpful!
    http://forums.riftgame.com/image.php?type=sigpic&userid=125779&dateline=13553  38065

  3. #3
    Telaran
    Join Date
    Oct 2011
    Posts
    86

    Default

    What I am looking for is the way to sort the secondary tables based on name.

    MainTable = {
    {name = "Name3", enabled = true, text1 = "Label3", cmd1 = "cmd3"},
    {name = "Name2", enabled = true, text1 = "Label2", cmd1 = "cmd2"},
    {name = "Name4", enabled = true, text1 = "Label4", cmd1 = "cmd4"},
    {name = "Name1", enabled = true, text1 = "Label1", cmd1 = "cmd1"},
    }

    If I use that routine nothing changes.
    for k,v in pairs(MainTable) do
    table.sort(v, function(a,b) return a.name < b.name end)
    end
    dump(MainTable)

  4. #4
    RIFT Guide Writer Noshei's Avatar
    Join Date
    Feb 2011
    Posts
    1,886

    Default

    give this a try

    *note it also helps to use the code tags when posting code.

    Code:
    table.sort(MainTable, function(a,b) return a.name < b.name end)

  5. #5
    Telaran
    Join Date
    Oct 2011
    Posts
    86

    Default

    Quote Originally Posted by Noshei View Post
    give this a try

    *note it also helps to use the code tags when posting code.

    Code:
    table.sort(MainTable, function(a,b) return a.name < b.name end)
    Thanks, that did it and I could have sworn I used that same routine before and it didnt work at all.

  6. #6
    Shield of Telara Adelea's Avatar
    Join Date
    Mar 2011
    Posts
    734

    Default

    Quote Originally Posted by TinnerKB View Post
    What I am looking for is the way to sort the secondary tables based on name.

    MainTable = {
    {name = "Name3", enabled = true, text1 = "Label3", cmd1 = "cmd3"},
    {name = "Name2", enabled = true, text1 = "Label2", cmd1 = "cmd2"},
    {name = "Name4", enabled = true, text1 = "Label4", cmd1 = "cmd4"},
    {name = "Name1", enabled = true, text1 = "Label1", cmd1 = "cmd1"},
    }

    If I use that routine nothing changes.
    for k,v in pairs(MainTable) do
    table.sort(v, function(a,b) return a.name < b.name end)
    end
    dump(MainTable)
    I know how to sort a table such as table[#].name but looking for something that is table[#][#].name
    You were looking for something to sort table[#].name, not table[#][#].name then =)
    http://forums.riftgame.com/image.php?type=sigpic&userid=125779&dateline=13553  38065

  7. #7
    Telaran
    Join Date
    Oct 2011
    Posts
    86

    Default Additional question

    Say I have a table..

    Code:
    MainTable = {
    {name = "Name3", enabled = true, time = 30},
    {name = "Name2", enabled = true, time = 28},
    {name = "Name4", enabled = true, time =14},
    {name = "Name1", enabled = true, time = 14},
    }
    If I use the routine below:

    Code:
    table.sort(MainTable, function(a,b) return a.time < b.time end)
    It sorts fine but entries with the same time flicker back and forth when shown in the ui. Is there a way to additionally sort by name as well to stop this?
    Last edited by TinnerKB; 08-19-2013 at 09:11 PM.

  8. #8
    Plane Touched
    Join Date
    Feb 2012
    Posts
    228

    Default

    Quote Originally Posted by TinnerKB View Post
    It sorts fine but entries with the same time flicker back and forth when shown in the ui. Is there a way to additionally sort by name as well to stop this?
    Code:
    table.sort(MainTable, function(a,b)
        if a.time ~= b.time then
          return a.time < b.time
        elseif a.name ~= b.name then
          return a.name < b.name
        -- ... other tie-breakers
        end
      end)
    Alternative version:

    Code:
    function multisort(fields)
      assert(type(fields) == "table", ("Error: expected `table' got `%s'."):format(type(fields)))
      return function(a,b)
        for _, field in ipairs(fields) do
          if a[field] ~= b[field] then
            return a[field] < b[field]
          end
        end
      end
    end
    
    table.sort(MainTable, multisort({ "time", "name" }))
    Last edited by Baanano; 08-19-2013 at 10:20 PM.

  9. #9
    Telaran
    Join Date
    Oct 2011
    Posts
    86

    Default

    Quote Originally Posted by Baanano View Post
    Code:
    table.sort(MainTable, function(a,b)
        if a.time ~= b.time then
          return a.time < b.time
        elseif a.name ~= b.name then
          return a.name < b.name
        -- ... other tie-breakers
        end
      end)
    Alternative version:

    Code:
    function multisort(fields)
      assert(type(fields) == "table", ("Error: expected `table' got `%s'."):format(type(fields)))
      return function(a,b)
        for _, field in ipairs(fields) do
          if a[field] ~= b[field] then
            return a[field] < b[field]
          end
        end
      end
    end
    
    table.sort(MainTable, multisort({ "time", "name" }))
    Thanks, I used the first example and so far have seen no flickering of the icons swapping. Much appreciated.

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts