Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Comparing and Ranking Children?

Asked by 8 years ago

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.

1
In what way to do you want to rank them? What is your specific order? BlueTaslem 18071 — 8y
0
table.sort is not reliable in that it may change equal elements' order. This will probably not be a problem for you. gskw 1046 — 8y
0
I would like to rank them from Highest to Lowest . Highest being 1st Lowest being Last. SamDomino 65 — 8y
1
Highest as far as height or what? fishguy100 135 — 8y
0
EXCELLENT EXPLANATION AND ANSWER!!! SamDomino 65 — 8y

1 answer

Log in to vote
2
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
8 years ago

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.

Ad

Answer this question