MIKI785 Posted August 12, 2012 Share Posted August 12, 2012 Hello, how can I sort table alphabetically? I know that I will probably use table.sort, but with what arguments? I want list of the players.. plrs = getElementsByType("player") table.sort(plrs, ?) --Problem Link to comment
unknooooown Posted August 12, 2012 Share Posted August 12, 2012 Hello, how can I sort table alphabetically? I know that I will probably use table.sort, but with what arguments?I want list of the players.. plrs = getElementsByType("player") table.sort(plrs, ?) --Problem You dont need any args for it. local t = { "b", "c", "a", "d" } print( unpack(t) ) -->> b c a d table.sort( t ) print( unpack(t) ) -->> a b c d EDIT: But then again, you cant just sort getElementsByType("player") afaik as that returns elements. If you want it to sort by the player names, you could do: local players = getElementsByType( "player" ) -- Get all the players. local playerNames = {} -- Create a table to store the player names. for i=1,#players do -- Loop through all players. local playerName = getPlayerName( players[i] ) -- Get the players name. table.insert( playerNames, playerName ) -- Store the players name in the new table. end table.sort( playerNames ) -- Sort the new table. print( unpack( playerNames ) ) -->> aPlayer bPlayer cPlayer dPlayer Link to comment
Buffalo Posted August 12, 2012 Share Posted August 12, 2012 I'm not sure but if you want to sort it correctly you should use table.sort(sortTable, function(a, b) return a < b end ) Link to comment
MIKI785 Posted August 12, 2012 Author Share Posted August 12, 2012 Wafamde, Ok, thanks. About the nicks, I know that, I just forgot that at the rush Buffalo, You're using < operator, i think that it will sort numbers only. Link to comment
Buffalo Posted August 12, 2012 Share Posted August 12, 2012 No, in this function (table.sort) operators < > sorts alphabetical values according to their ASCII values. Previous example works perfectly, i was just wondering if it will sort like this without using operator < and i think it wouldn't. Link to comment
MIKI785 Posted August 12, 2012 Author Share Posted August 12, 2012 Ok, this is my first time working with table.sort... thanks. Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now