Jump to content
Alloder.pro  about Allods with love 😱
Search In
  • More options...
Find results that contain...
Find results in...

Servers monitoring and the Addons Editor

We present you two legends. All dreams come true.

Servers monitoring The Addons Editor

Digest April

We talk about what was done and updated in the past month. We help keep abreast of events.

Read more

Game tooltips

Tooltips provide a way for 3rd party fansites and extensions to display detailed information on mouseover.

Read more

How to sort a string-indexed-table after the strings in the indexes?


Guest Carnifex
 Share

Recommended Posts

Guest Carnifex

So, I have a table similar to this:

t={["playername1"]=1,["playername2"]=0,["playername3"]=0,["playername4"]=0}

and i want to sort this table in lexicographical order after the strings=player-names in the indexes of the table.

How can I do this, because with "for i,m in t..." I get a seemingly random order of the playernames?

BTW: I need this for my new addon TS3Viewer.

Link to comment
Share on other sites

Guest Carnifex

no, they are string indexed (ts does not know wstrings), but they are sorted in the for loop not after the alphabet, but in the row I write them into the table

so for example:

t["d"]=1

t["a"]=2

t["z"]=0

for n,i in t do

LogInfo(n)

end

returns

d

a

z

and not

a

d

z

Link to comment
Share on other sites

Code:
t = {}
t ["d"] =1
t ["a"] =2
t ["z"] =0

local function Sort( tab, dir )
local t = {}
for k in pairs( tab ) do table.insert( t, k ) end
if dir then table.sort( t, function( a, b ) return a > b end ) else table.sort( t ) end
--table.sort( t, function( a, b ) return dir and a > b or a < b end )
for _, v in ipairs( t ) do LogInfo( tab [ v ]  ) end
end

Sort( t )


upd: Commented line was replaced by a common if-else statement, because, in this case, using and-or construction with existing "dir" breaks the sort function.
Link to comment
Share on other sites

Sorting by VALUES it extremely easy, like:

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

but sorting by KEYS is more tricky:

http://lua-users.org/wiki/SortedIteration

Anyway, you will need some extra function(s) to do that. But Duvodas provided a much simplier solution.

P.S. Don't look at GuildGui1b TERRIBLE sorting function. It is the WORST example, because it sorts by VALUES without use of table.sort(). It just needs a total rewrite, but I was too lazy to touch it yet... There are good sorting examples in DarkDPSMeter.

Link to comment
Share on other sites

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

By using our site you agree to the Terms of Use