How can I sort a friend list of ID, data pairs in ABC order?

I made a function that returns player’s friends’ data dictionary table.
the table looks like(for example:
[123456] = { [“DisplayName”] = “Alpha”, [“Username”] = “alpha123” … }
[789012] = { [“DisplayName”] = “Beta”, [“Username”] = “beta_456” … }

And I want to sort them in ABC order, based on the player’s DisplayName.

any answer thank you so much!

1 Like

This is a very simple fix I think
Make a template frame and a scrolling and use a list layout it sorts alphabetically so just name the frames the players name. If your trying to make it a gui

2 Likes

I recommand making the userid the display name or username of the player. In that case we can do this:

local tbl = {
	["Beta"] = { 
		
	},
	
	["Alpha"] = {
	
	}
}

table.sort(tbl, function(tableName1, tableName2)
	return string.lower(tableName1) < string.lower(tableName2)
end)

for i, v in tbl do
	print(v) 
end

1 Like

I thinked about this problem and realized: Cannot sort a Dictionary table itself. only Arrays can be sorted! It was silly of me to try to sort a dictionary table.
Even more if value is another dictionary table.

Yes I forgot that i don’t need to sort in table because it will only used to make friend list gui.

This can also be a solution!

so I just set UIListlayout.SortOrder to Name.
cannot sort dictionary table.

  • If you really need to sort(by abc order) friend data dictionary,
    transform dictionary table to array and try table.sort, try table.sort
local friendsDict = {} --{[ID] = {datas...}}
local friendsArray = {}

for id, friendDatas in pairs(friendsDict) do 
    friendDatas["ID"] = id--if there is no userID in friendDatas, add
    table.insert(friendsArray, friendDatas)--insert every friend data to array
end

table.sort(friendArray, function(a, b)
    return string.upper(a.Displayname) < string.upper(b.Displayname) --uppercase each displayname and compare it
end)
--may contain some errors, I was half asleep

Sorry for replying late, translating took long time.
Thank you for help me!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.