So I’m making a player list and want the names in alphabetical order.
How would I get the names in alphabetical order?
Would love some ideas and answers!
You could translate the different letters to as I numbers and compare those. So once you have those numbers, just sort.
table.sort is your friend
local players = game:GetService( 'Players' ):GetPlayers()
table.sort( players, function ( a, b )
-- you should return true when a comes before b
return a.Name:lower() < b.Name:lower()
end )
Comparison operators work on strings by comparing their string.byte values for each character. If you want to treat lower and uppercase equally, you need the :lower() part otherwise lowercase will all come after uppercase. If you want them separate, just use a.Name and b.Name.
Your table is now sorted alphabetically ascending (a-z), so players[ 1 ]
is now the first name in alphabetical order. Flip the sign to >
to get it descending (z-a).
Thank you for your help!
No worries. Don’t forget to mark solution on the reply if it solved your issue.
This is what I want:
What I got:
How did you take your sorted table and create the buttons? Also what is their container?
If you use a UIListLayout then set the LayoutOrder of the buttons to their index in the table, and ensure the UIListLayout’s SortOrder is set to LayoutOrder, not Name.
for index, player in ipairs( players ) do
-- create your button
button.LayoutOrder = index
button.Text = player.Name
end
If you set the Y position manually inside of a scrolling frame, set the position based on index in the loop above. In some weird edge cases the for loop doesn’t go through the table sequentially, so you need to use the index.
This is probably a gui sorting issue.
If you’re using one of the UI layout objects (like “UI Grid Style Layout”) their should be a ‘sort by’ option.