Ordering objects based on attribute

Is there a way I can put an object inside of a table and order it based on what its number attribute returns?

basically, a loop gets a bunch of objects with attributes, each with a number inside of the range of 1-7, then it is put into the table and ordered based on what the attribute returns. so if the attribute returns 1, then the object is in the first place, if the attribute returns 5, then the object is in the fifth place.

Yeah you can just use table.sort

However this time instead of sorting based on the elements second index like the example code a[2] you use a:GetAttribute(“Value”)

1 Like

i’ve just given it a try and it’s spitting out an attempt to index nil with 'GetAttribute'
all the objects that are being put into the table for sure do have a number attribute called Placement

snippet of the code

table.sort(stationsTable,function(a, b)
	return a:GetAttribute("Placement") > b:GetAttribute("Placement")
end)

Works fine for me make sure all the instances in the table have an attribute.