How to sort a dictionary with named values?

I am trying to sort a dictionary with named values from greatest to least.

If this is my table:

{
  ["Apples"] = 45,
  ["Bananas"] = 89,
  ["Oranges"] = 34,
  ["Pears"] = 95,
}

How could I sort the values from greatest to least, so the table looks like this?

{
  ["Pears"] = 95,
  ["Bananas"] = 89,
  ["Apples"] = 45,
  ["Oranges"] = 34,
}

Thanks in advance!

This is not an array.
This is a dictionary.

You could switch that into an array with tables inside.
Example:

local array = {
    {"Banana",10},   
    {"Apple",25},
}

You can then use table.sort on this and return whatever you want( for e.g: the 2nd value which is a number in an order)

3 Likes

You can’t. Dictionaries don’t have a particular sort to them. A workaround for this might be to have an array which stores the keys of your dictionary. You could sort your array based on the values in your dictionary.