How can i sort this table?

Basically i am just trying to sort this table from highest chance to lowest chance but i dont know how to do it i tried to do it with table.sort but i failed anyways here is the table:

local RNG = {
	Money1 = {
		Chance = 1,
		Name = "Money1",
	},
	Money2 = {
		Chance = 3,
		Name = "Money2",
	},
	Money3 = {
		Chance = 6,
		Name = "Money3",
	},
	Money4 = {
		Chance = 14,
		Name = "Money4",
	},
	Money5 = {
		Chance = 25,
		Name = "Money5",
	},
}
1 Like

Could someone maybe help me please?

First you will have to convert the dictionary to array by doing this:

local RNG = {
	[1] = {
		Chance = 1,
		Name = "Money1",
	},
	[2] = {
		Chance = 3,
		Name = "Money2",
	},
	[3] = {
		Chance = 6,
		Name = "Money3",
	},
	[4] = {
		Chance = 14,
		Name = "Money4",
	},
	[5] = {
		Chance = 25,
		Name = "Money5",
	},
}

and then you will need to use table.sort
Here is how you do it:

table.sort(RNG, function(A, B)
    return A.Chance > B.Chance
end)
4 Likes

I tried it and it works thank you so much!

1 Like

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