Guest Carnifex Posted January 29, 2011 Share Posted January 29, 2011 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. Quote Link to comment Share on other sites More sharing options...
SLA Posted January 29, 2011 Share Posted January 29, 2011 Are you sure it is REALLY string-indexed? (NOT WString-indexed, because WStrings are userdata) ? If there are WString indexes, then it is epic fail. Quote Link to comment Share on other sites More sharing options...
Guest Carnifex Posted January 29, 2011 Share Posted January 29, 2011 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 Quote Link to comment Share on other sites More sharing options...
duvo Posted January 29, 2011 Share Posted January 29, 2011 Code: t = {}t ["d"] =1t ["a"] =2t ["z"] =0local 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 ] ) endendSort( 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. Quote Link to comment Share on other sites More sharing options...
SLA Posted January 29, 2011 Share Posted January 29, 2011 It should work. Quote Link to comment Share on other sites More sharing options...
Guest Carnifex Posted January 29, 2011 Share Posted January 29, 2011 Yes, I've also had a similar idea of a alternative sorting algorithm. But there is no solution for sorting it in-place, right? Quote Link to comment Share on other sites More sharing options...
SLA Posted January 29, 2011 Share Posted January 29, 2011 Sorting by VALUES it extremely easy, like: Code: table.sort( t, function(a, 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. Quote Link to comment Share on other sites More sharing options...
Guest Carnifex Posted February 1, 2011 Share Posted February 1, 2011 BTW: Is there with our access to lua models a way to check, if the file exists before running dofile(file)? Quote Link to comment Share on other sites More sharing options...
Recommended Posts