Problem with sorting table

Hello! I try to sort cases in table, which i recieved from modulescript. I have this cases:

["Case"] = {
		["Basic"] = {
			["Cost"] = 10,
			["Name"] = "Basic case",
			["ImageID"] = 1234567,
			["Drop"] = {
				["Basic"] = 80,
				["Rare"] = 20,
				["Super"] = 1
			}
		},
		["Rare"] = {
			["Cost"] = 20,
			["Name"] = "Rare case",
			["ImageID"] = 1234567,
			["Drop"] = {
				["Basic"] = 20,
				["Rare"] = 80,
				["Super"] = 3
			}
		},
		["Cool"] = {
			["Cost"] = 40,
			["Name"] = "Super case",
			["ImageID"] = 1234567,
			["Drop"] = {
				["Basic"] = 0,
				["Rare"] = 60,
				["Super"] = 40
			}
		},
		["Imagine"] = {
			["Cost"] = 100,
			["Name"] = "Godly case",
			["ImageID"] = 1234567,
			["Drop"] = {
				["Basic"] = 0,
				["Rare"] = 0,
				["Super"] = 100
			}
		},
	}

I need to sort it from lower to higher by it’s cost and make a frame with it. Here’s my frame making code:

local rq = require(game.ReplicatedStorage.Modules[""])
local cnt = 1
function sortTable(a, b)
	return a["Cost"] < b["Cost"]
end
local norm = rq.Case
table.sort(norm, sortTable)
for _,case in norm do
	print(case)
	local frm = game.StarterGui.Frame:Clone()
	frm.Parent = script.Parent
	frm.TextLabel.Text = case.Name
	frm.ImageLabel.Image = case.ImageID
	frm.TextLabe.Text = case.Cost
	if cnt == 4 then cnt = 1 end
	frm.Position = UDim2.new((0.015+0.3*(cnt-1)),0,(0.03+(0.25*math.round(#script.Parent:GetChildren()/3-1))),0)
	cnt += 1	
	
	print(frm.Position)
end

But here’s the frame’s i getting:


I wanna frame’s to be like: 10,20,40,100, not reversed. How to do it?

try make the “<” in the sorttable function into a “>”

arleady try, not worked

Summary

30 letters lelelelelellelelelelele

Dictionaries don’t have an order to them. If you must have a dictionary and you must have an ordering, then you must use a separate index.

local index = {"Basic", "Rare", "Cool", "Imagine"}
table.sort(index, function(a, b) return norm[a].Cost < norm[b].Cost end)
print(norm[index[1]])
4 Likes

The problem is most likely with the UI’s layouts. You should change the LayoutOrder of the frames to the Cost. So for example, whenever you create the UIs you should do:

local frm = game.StarterGui.Frame:Clone()
frm.Parent = script.Parent
frm.TextLabel.Text = case.Name
frm.ImageLabel.Image = case.ImageID
frm.TextLabe.Text = case.Cost
frm.LayoutOrder = case.Cost

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