Sort a Dictionary

Hiwi! Thank you for reading.
Can someone show me some approaches to sort this Dictionary, no matter ascending or descending, just some approaches to sort it?

local Dic = {}
Dic["156432156"] = {Points = 500, OtherStuff = X}
Dic["234235345"] = {Points = 1100, OtherStuff = X}
Dic["5467456734"] = {Points = 200, OtherStuff = X}

I already did it with my own approach, I just wanna know better ones, can you pls share how you would do it?

Hey! If you’re planning on making a Leaderboard you should take a look at OrderedDataStore | Roblox Creator Documentation. Hopefully this article helps you out. There is an example at the bottom of the page.

Cheers

1 Like

Thank you so much!, but thats not the required scenario in this case. Its not possible to save an OrderedDataStore for a small task like this one.
I just want to find a way to sort a dictionary

Ohh, I see!

Well, then you could technically achieve this with table.sort even if you tried this already.
Important to know with table.sort is, it doesn’t return a table. It replaces the old one.

So this is an example of how to use table.sort:

table.sort(Dic, function(Player1, Player2)
	return Player1.Points > Player2.Points
end)

Maybe this helps for a solution with your problem, let me know if it does or not!
If it doesn’t, maybe also send me a snippet of your table.sort code.

Cheers

1 Like

Yup, thats exactly what I did, sort the array gotten from the dictionary, then use that sorted array to match it with the dictionary, creating a new dictionary with all players and points ordered. But, if some players has the same amount of points, I bet, weird things will occur.

Im asking for some other approaches to sort a dictionary

I don’t exactly understand what you mean, but would changing

to >= solve your issue?

I think thats just a small typo. My issue its not related with using the table.sort with a
function

Just this, can someone show me some approaches to sort this Dictionary, no matter ascending or descending, just some approaches to sort it?

local Dic = {}
Dic["156432156"] = {Points = 500, OtherStuff = X}
Dic["234235345"] = {Points = 1100, OtherStuff = X}
Dic["5467456734"] = {Points = 200, OtherStuff = X}

Dictionaries do not have any real order & so attempting to use table.sort by itself probably holds no promise. You will definitely need to do a little extra work like you’ve been trying to.

For sorting of dictionaries I have always used this method that 1waffle1 goes into detail about here:

I see no reason this could not work for your use-case because the sorted variant will still hold the indexes thus making it easy to reattach “OtherStuff” on a per index basis after sorting.

1 Like

This could need some work with a recursive function’s.

1 Like
local function Sort(table)
    for I,v in pairs(table) do
        if typeof(v) == "table" then
            Sort(v)
        end
    end
end
1 Like

Its just, thats what I’ve been doing, when Players has the same amount of Points, what happens? I think weird stuff occurs, thats all. I just wanna know some new approaches to sort a dictionary