Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Comparing and Ranking Children?

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.

5 comments

1

In what way to do you want to rank them? What is your specific order?

0

table.sort is not reliable in that it may change equal elements' order. This will probably not be a problem for you.

0

I would like to rank them from Highest to Lowest . Highest being 1st Lowest being Last.

1

Highest as far as height or what?

0

EXCELLENT EXPLANATION AND ANSWER!!!

1 answer

✓ Accepted answer

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.