Hello, I am trying to make a function that compares every child to every other child in the most efficient way possible and rank them. (1st,2nd,3rd...)
I would have considered table.sort(Table,pos,comp) but I heard that it is not always reliable from lua.org
Please help, I have been looking everywhere for a way to compare a given set of points and rank them in a specific order.
table.sort(list, lessThan)
sorts list
in increasing order by consulting lessThan(a, b)
to compute whether or not a < b
.
Thus if you want to, for example, compare Vector3s by their Y-value, ascending, you could use this as your lessThan
function:
function vectorYCompare(a, b) return a.y < b.y end
If you wanted the sort to be descending, you'd have to flip the <
into a >
:
function vectorYCompareDescending(a, b) return a.y > b.y end
If you actually had a list of Parts but you wanted to compare their locations,
function descendingParts(a, b) return a.Position.y > b.Position.y end
table.sort
isn't stable. That means if you have "equal" elements (in this case, parts with the same Y
), their order after doing the sort is undefined.
Unless you got the parts in some particular order (:GetChildren()
won't give you in any particular order), this doesn't matter.