ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Enter a Score and work out its postion?

COW Forums : Adobe After Effects

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Dave Martin
Enter a Score and work out its postion?
on Jul 31, 2013 at 9:30:18 pm

Here's a challenge for ya...

I'm building a scoreboard and i'd like to have the postion of the contestant worked out based on their score

So, say I have these variables;
Name1 = [score]
Name2 = [score]
Name3 = [score]
Name4 = [score]

How can I work out the postions (1st, 2nd, 3rd, 4th,...) based on the scores?
I would like an expression to return values of 1, 2, 3, 4,... Which i will use to change the Y position of a pre Comp in the main comp.

I've got loads of these boards to do so was hoping I could just build one. Duplicate it, Enter the scores and have the contestants re-ordered automatically :)


Return to posts index

Walter Soyka
Re: Enter a Score and work out its postion?
on Aug 1, 2013 at 12:30:43 pm

Here's an expression that may help you. Be sure to read the comments and do something with the value at the end:

// scores go here!
scores = [300,800,200,500,0,500];

// create a new array of arrays which will hold hold the scores and their original index in the array.
scoresIndices = new Array(scores.length);
for (i = 0; i < scores.length; i++) scoresIndices[i] = [scores[i],i];

// sort the array, like magic
scoresIndices.sort(function(a,b) {return b[0] - a[0]});

// finally, you can get the indicies in sort order. The first will be scoresIndices[0][1], the second will be scoresIndicies[1][1]
// the third will be scoresIndices[2][1] and so on

Walter Soyka
Principal & Designer at Keen Live
Motion Graphics, Widescreen Events, Presentation Design, and Consulting
RenderBreak Blog - What I'm thinking when my workstation's thinking
Creative Cow Forum Host: Live & Stage Events

Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2018 All Rights Reserved