Table.sort via item price not working?

  1. What do you want to achieve?
    Table to sort by the price.

  2. What is the issue?
    Table sorting is not working, and i cant see what the problem is even though its super simple.

  3. What solutions have you tried so far?
    Looked at multiple posts on this issue, couldnt understand why mine didnt work.

	local toolProperties = {tool.Name, tool.Price.Value}
	table.insert(tools, toolProperties)
	
	table.sort(tools , function(a , b)
		return a[2] > b[2]
	end)

	for _,v in pairs(tools) do
		print(v[1], v[2])
	end

This is cut out from my local script, everything seen here is labelled right.

image
I know theres alot of posts about it but i just cant figure out what the problem is. (Im am new to tables)

2 Likes

you have to make another table, where the indexed price would be a tool price value, then you doing for loop and simply sorting value from smallest to biggest

1 Like

tools should look somewhat like this (with unspecified order):

local tools = {
	{"Flashlight", 50};
	{"Lazer cutters", 500};
	{"Bolt cutters", 150};
	{"Weight", 100};
	{"Knife", 150};
	{"Bouy", 100};
}

… where sorting like that works fine (in your case highest to lowest value).

Perhaps it’s pairs that doesn’t necessarily keep the order.

Replace it with ipairs or just use generalized iteration.

for _,v in tools do
	print(v[1], v[2])
end
1 Like

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