1

I have made a LUA table like so for my scoreboard:

Score.Ranks = {}

Score.Ranks["superadmin"]     = {name = "Boss", col = Color(183, 109, 253) }
Score.Ranks["operator"]       = {name = "Chief", col = Color(254, 129, 1) }
Score.Ranks["commander"]      = {name = "Commander", col = Color(222, 54, 54) }
Score.Ranks["moderator"]      = {name = "Director", col = Color(53, 225, 227) }

I want to sort this Table such that the 'names' are sorted in the following order when displayed on my scoreboard:

Boss
Director
Commander
Chief

How can I do this?

4
  • there was a small dispute: do you want to sort by "name" field or some manual sorting? because "Director" should be in last place alphabetically in your example.
    – Mike V.
    Commented Jun 29, 2020 at 16:01
  • I want to sort by name field or the super admin operator commander fields whichever sets the ordinarily.
    – therion
    Commented Jun 29, 2020 at 18:20
  • You can't really sort hash tables. Not lua's implementation of them at least. So "such that the 'names' are sorted" doesn't make sense. It is unknown, how the scoreboard is displayed. If you want specific order, just use array.
    – Dimitry
    Commented Jun 30, 2020 at 5:02
  • Your sort doesn't make sense to me. What are you sorting by to make 'Boss' appear first, the name or rank? Why does 'Commander' appear after 'Director'? If there is something in your head that makes that happen, it needs to be spelled out. Commented Jul 13, 2020 at 13:29

2 Answers 2

2

you can only sort a table with numeric indexes,so first convert the table:

Score.Ranks = {}

Score.Ranks["operator"]       = {name = "Chief", col = Color(254, 129, 1) }
Score.Ranks["superadmin"]     = {name = "Boss", col = Color(183, 109, 253) }
Score.Ranks["commander"]      = {name = "Commander", col = Color(222, 54, 54) }
Score.Ranks["moderator"]      = {name = "Director", col = Color(53, 225, 227) }

local tmp_t = {}
for k,v in pairs(Score.Ranks) do
    local rank = (k=="superadmin") and 1 or (k=="operator" and 3 or 2) 
    table.insert(tmp_t, { rank = rank .. v.name, key = k, name = v.name, col = v.col} )
end

then the table is sorted by name and then use it.

    table.sort(tmp_t ,  function (a, b) return  (a.rank <  b.rank ) end)        
    for k,v in pairs(tmp_t) do
         print(k , v.rank, v.key, v.name)
    end
 
6
  • how does this solve the problem? besides that there are already countless answers on how to sort tables by keys you could link, there are 4 table elements without any obvious order. so befor you can do any sorting you have to give each of the 4 keys a priority which already sorts the table manually. also Lua's table.sort already implements a quicksort in C. why do it yourself in Lua?
    – Piglet
    Commented Jun 29, 2020 at 8:20
  • @Piglet you are right, I removed the unwanted quicksort
    – Mike V.
    Commented Jun 29, 2020 at 8:50
  • still your answer is pointless. I mean it's a good answer qualitywise but it doesn't solve the problem. he doesn't want to sort by name. he want's a specific order for those names which cannot be derived from the keys or names alphabetically and hence has to be done manually.
    – Piglet
    Commented Jun 29, 2020 at 8:52
  • @Piglet I want to sort by name or the 4 classes operator super admin etc. The super admin should be on the top and operator at the bottom.
    – therion
    Commented Jun 29, 2020 at 18:22
  • yeah but somehow you don't understand my answer. sorting 4 things is pointless if you have to give them a priority manually...
    – Piglet
    Commented Jun 29, 2020 at 18:59
0

As you only have 4 items I'd simply sort them by hand.

print("Scoreboard:")
print(Score.Ranks.superadmin.name)
print(Score.Ranks.moderator.name)
print(Score.Ranks.commander.name)
print(Score.Ranks.operator.name)

You have to tell your computer that superadmin comes befor moderator in order to sort it later. But if you have to enter a priority for each entry anyway, there is no need to sort. Its not that you could derive an order from any property.

2
  • It's an addon of which I am trying to modify 'config.lua'. When I press tab in game, it displays a scoreboard with unsorted rank order. I am trying to sord that rank order. So I am not sure if I can use the print command there.
    – therion
    Commented Jun 27, 2020 at 12:20
  • print is just an example. I don't know what you use to display the score board. Scroe.Ranks cannot be sorted as it does not have an order. You have to define in which order to use those entries. But then there is no need for automatic sorting.
    – Piglet
    Commented Jun 27, 2020 at 12:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.