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

New program for writers

We turn from quantity to quality and tell you how we will supplement the Allods Team program with rewards in rubles.

More

The new Updater

Let us to introduce the new addon updater software and to share the details

Read more

Alloder 2.0

We have started the process of project evolve, and this relates not only, and not even primarily of the site's view

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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