How do I order numbers by value?

Hi, I want to order numbers, meaning get a number value for what order the number is, of a list of numbers.

Example: Data = {1,6,3,8,4}
1 = 1 (order), 6 = 4 (order), 3 = 2 (order), 8 = 5 (order), 4 = 3 (order)

you would use table.sort() which orders by Value

table.sort(Ex, function(a, b)
   return a > b -- order of sorting
 -- you can also do: a < b
end

You can take the set inverse since its already an array:

--Returns the set inverse if it exists
function Inverse(set)
	local result = {}

	for k, v in set do
		result[v] = k
	end

	return result
end
1 Like

Hey can you explain exactly what this is. I just want to make a script that you can put in a list of data, and it makes a number value for each one with the value of order.

Hey can you explain exactly what this is as well lol. I just want to make a script that you can put in a list of data, and it makes a number value for each one with the value of order.

local AllNumbers = {}
local x = 1
local CostMultiplier = 3
for i,v in pairs(script.Parent:GetChildren()) do
	if v:IsA("Folder") then
		local Pickaxe = v["Pickaxe_Template"]
		local Mesh = Pickaxe.Mesh
		local OtherMesh = Pickaxe.Parent.Pickaxe.Value.Mesh
		Mesh.TextureId = OtherMesh.TextureId
		Mesh.MeshId = OtherMesh.MeshId
		local Number = Instance.new("NumberValue", v)
		Number.Name = "Order"
		Number.Value = x
		x += 1
		table.insert(AllNumbers, Number)
	end
end


This is my current script. I want to make the order value based off x.Cost.Value.

This is what he (Maybe she) is doing:

k is the Key to the Table

v is the Value of the key

result[v] is creating a Value for the key, but v is the value of another tables key.
= k is the key in the other table, which is now the value

table.sort(AllNumbers, function(a,b) return a.Cost.Value > b.Cost.Value end)
1 Like

Wait hold up back up why are you putting the numbers into a table and not doing anything with the Picaxe or Mesh or OtherMesh?

image

local AllNumbers = {}
local x = 1
local CostMultiplier = 3
for i,v in pairs(script.Parent:GetChildren()) do
	if v:IsA("Folder") then
		if v:FindFirstChild("Cost") then
			local Pickaxe = v["Pickaxe_Template"]
			local Mesh = Pickaxe.Mesh
			local OtherMesh = Pickaxe.Parent.Pickaxe.Value.Mesh
			Mesh.TextureId = OtherMesh.TextureId
			local Cost = v.Cost
			Mesh.MeshId = OtherMesh.MeshId
			local Number = Instance.new("NumberValue", v)
			Number.Name = "Order"
			Number.Value = x
			x += 1
			table.insert(AllNumbers, Cost)
		end
	end
end

table.sort(AllNumbers, function(a,b) return a.Value < b.Value end)
print(AllNumbers)

Any ideas why its printing just Cost? nvm I had a goofy moment there, wait so I just print table.sort etc etc?

Your list AllNumbers is just a list of numbervalues. What else would it print?

1 Like
local AllNumbers = {}
local x = 1
local CostMultiplier = 3
for i,v in pairs(script.Parent:GetChildren()) do
    if v:IsA("Folder") then
        if v:FindFirstChild("Cost") then
            local Pickaxe = v["Pickaxe_Template"]
            local Mesh = Pickaxe.Mesh
            local OtherMesh = Pickaxe.Parent.Pickaxe.Value.Mesh
            Mesh.TextureId = OtherMesh.TextureId
            local Cost = v.Cost
            Mesh.MeshId = OtherMesh.MeshId
            local Number = Instance.new("NumberValue", v)
            Number.Name = "Order"
            Number.Value = x
            x += 1
            table.insert(AllNumbers, Cost)
        end
    end
end

local Data = table.sort(AllNumbers, function(a,b) return a.Value < b.Value end)
print(Data)

New script, still printing nil

I still don’t understand why you’re only putting the Cost NumberValue(?) into the list, why not put each “v” into the list and then sort it by v.Cost.Value?

AllPicks = {}
for i,v in pairs(script.Parent:GetChildren()) do
    if v:IsA("Folder") and v:FindFirstChild("Cost") then
        table.insert(AllPicks, v)
    end
end

table.sort(AllPicks, function(a,b) return a.Cost.Value > b.Cost.Value end)
1 Like

But how do I use this to change the order. No one has answered this. :cry: like I just want a value called order and then change the order to that.

Can you back up and explain what you actually want to happen. Like input → output. Draw a picture or something?

1 Like

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