Jump to content

Sorting table alphabetically


MIKI785

Recommended Posts

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...