Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Is there any way to Alphabetize Gui Buttons?

Asked by 7 years ago

I have a GUI menu with lots of GUI textbuttons, and currently they are placed pretty randomly (its a scrolling gui) and I would like to organize them Alphabetically via a script. I would like to include an attempt at this, but to be honest I have no earthly where to start since I don't know if there is even a function or way to do this. I assume I would need to put all the buttons in a table and then call some kind of function that places them in the scrolling frame according to their name but I'm not really sure. Would it involve assigning letters to numbers like A = 1 B = 2 Etc, and then having a set distance as a variable for the space between the buttons and adding a certain number of spaces depending on which number(letter) it is? If so how would I tell the script to recognize the first letter of each word? Sorry if this is confusing, let me know if I need to clarify anything. Thanks guys!

0
This is entirely possible, and there are lots of ways to do it. Your question makes perfect sense. I've suggested one possible way in my answer, but you can also implement your own sorting as you described with tables and manual positioning. duckwit 1404 — 7y

2 answers

Log in to vote
1
Answered by
duckwit 1404 Moderation Voter
7 years ago

Recently (January 20th 2017), ROBLOX introduced a new instance type called UILayout whose subclasses provide basic high-level layouts for GuiObjects like grids and lists.

Have a look at the API definition for UIListLayout on the Official ROBLOX Wiki.

It has some basic instructions about how to use it, but let me draw your attention to the SortOrder field. One of the options for this is Custom, which means you can specify your own sorting function. There is also a short description of how to setup your own sort function.

I've never used this before, so it's a tad experimental, but the gist of it is this:

  1. Add a UILayoutObject as a sibling of the objects which you want to be listed alphabetically
  2. Define a custom sorting function. You'll probably want to sort the Name field of GuiObject lexicographically. Lexicographical sorting is a fancy name for the intuitive dictionary-like sorting of text ('a' < 'b', 'cat' < 'dog', etc...). Lua implements this by default for string comparison.
function CompareGui(a, b)
    return a.Name < b.Name
end
  1. Set this custom comparator as the sorting function of the list layout:
myListLayout.SetCustomSortFunction(CompareGui)
  1. Set the SortOrder of that list layout object to Custom, like this:
myListLayout.SortOrder = Enum.SortOrder.Custom

Voila! You should have an alphabetically sorted list. You may want to sort on the Text property of your GuiObjects instead of Name, though. It's up to you.

I haven't tested this API before, so there might be some quirks to work out, but that's a rough guide to get you started.

1
This is exactly what I was asking for, time to get to work haha thanks! Gwolflover 80 — 7y
Ad
Log in to vote
0
Answered by 7 years ago

your question is quite confusing lol but, i try

local yourtable = {
    [1] = your.thing.here,
    [2] = your.thing.here,
    [3] = your.thing.here,
    [4] = your.thing.here,
    [5] = your.thing.here,
    [6] = your.thing.here,
    [7] = your.thing.here,
    [8] = your.thing.here,
    [9] = your.thing.here,
    [10] = your.thing.here,
    [11] = your.thing.here,
    [12] = your.thing.here,
}

for i,v in pairs(yourtable) do -- i,v mean index,value
    print(v) --would print the value(second parameter of the table) e.g your.thing.here
    print(i) --would print the index(first parameter of the table) e.g 7
end

you can try what you want its not very clear o3o

0
What I'm asking for probably doesn't exist, so this is probably the closest thing to it, thanks! Gwolflover 80 — 7y
0
I marked this answer down because the syntax that you've used to instantiate the elements of the table is poor practice. If you decide to remove the 1st element, you have to change all of the other numbers. Instead just use {thing1, thing2, thing3, etc..}, no need to specify indices explicitly. duckwit 1404 — 7y

Answer this question