Sorting a dictionary by the value

Hi, how can I sort this:

local Results = {
  Player1 = "Collected",
  Player2 = "Not collected",
  Player3 = "Collected"
}

Into this:

local Results = {
  Player1 = "Collected",
  Player3 = "Collected",
  Player2 = "Not collected",
}

Dictionaries are inherently unsorted, but I can’t see why you’d need to sort a dictionary. What I do suggest is creating a custom Enum system.

i.e.

local SortedEnums = {
    Collected = 1;
    ['Not Collected'] = 2;
}

local Results = {
    Player1 = SortedEnums.Collected;
    Player2 = SortedEnums.Collected;
    Player3 = SortedEnums['Not Collected']
}

Then do whatever you need from there. Using enums like this helps to make the data easier to understand in a “sorting” scheme.

What’s your use case for sorting a dictionary? You should never be relying on order when using a dictionary whether or not you come up with a solution to sort it. It’s better to address your underlying problem (why does it need to be sorted?) rather than an attempted solution for your problem (how do you sort a dictionary by value?). Treads on XY problem grounds.

Yeah I know, I realized sorting the dictionary would be inefficient. Instead I did a workaround which uses arrays that is more reliable.