Skip to content

Instantly share code, notes, and snippets.

@MarissaJ
Created November 20, 2018 22:57
Show Gist options
  • Save MarissaJ/752e49f887e8d8832b54483b374df1cd to your computer and use it in GitHub Desktop.
Save MarissaJ/752e49f887e8d8832b54483b374df1cd to your computer and use it in GitHub Desktop.
After Effects 2019 Data-driven Expression- Sort text with corresponding number data
var examples = [
{
'text' : thisComp.layer("Data-Driven-Sample.csv")("Data")("Outline")("Text Data")("Text Data 0"),
'number' : parseInt(thisComp.layer("Data-Driven-Sample.csv")("Data")("Outline")("Number Data")("Number Data 0"))
},
{
'text' : thisComp.layer("Data-Driven-Sample.csv")("Data")("Outline")("Text Data")("Text Data 1"),
'number' : parseInt(thisComp.layer("Data-Driven-Sample.csv")("Data")("Outline")("Number Data")("Number Data 1"))
},
{
'text' : thisComp.layer("Data-Driven-Sample.csv")("Data")("Outline")("Text Data")("Text Data 2"),
'number' : parseInt(thisComp.layer("Data-Driven-Sample.csv")("Data")("Outline")("Number Data")("Number Data 2"))
},
{
'text' : thisComp.layer("Data-Driven-Sample.csv")("Data")("Outline")("Text Data")("Text Data 3"),
'number' : parseInt(thisComp.layer("Data-Driven-Sample.csv")("Data")("Outline")("Number Data")("Number Data 3"))
},
{
'text' : thisComp.layer("Data-Driven-Sample.csv")("Data")("Outline")("Text Data")("Text Data 4"),
'number' : parseInt(thisComp.layer("Data-Driven-Sample.csv")("Data")("Outline")("Number Data")("Number Data 4"))
},
{
'text' : thisComp.layer("Data-Driven-Sample.csv")("Data")("Outline")("Text Data")("Text Data 5"),
'number' : parseInt(thisComp.layer("Data-Driven-Sample.csv")("Data")("Outline")("Number Data")("Number Data 5"))
},
{
'text' : thisComp.layer("Data-Driven-Sample.csv")("Data")("Outline")("Text Data")("Text Data 6"),
'number' : parseInt(thisComp.layer("Data-Driven-Sample.csv")("Data")("Outline")("Number Data")("Number Data 6"))
},
{
'text' : thisComp.layer("Data-Driven-Sample.csv")("Data")("Outline")("Text Data")("Text Data 7"),
'number' : parseInt(thisComp.layer("Data-Driven-Sample.csv")("Data")("Outline")("Number Data")("Number Data 7"))
},
{
'text' : thisComp.layer("Data-Driven-Sample.csv")("Data")("Outline")("Text Data")("Text Data 8"),
'number' : parseInt(thisComp.layer("Data-Driven-Sample.csv")("Data")("Outline")("Number Data")("Number Data 8"))
},
{
'text' : thisComp.layer("Data-Driven-Sample.csv")("Data")("Outline")("Text Data")("Text Data 9"),
'number' : parseInt(thisComp.layer("Data-Driven-Sample.csv")("Data")("Outline")("Number Data")("Number Data 9"))
},
];
const list = examples.sort((a,b) => b.number-a.number).map((examples, number, array) => examples.text)
list.join("\n")
@volition74
Copy link

Hi Marissa,

I learnt a lot going through this post ages ago, and I had to revisit it for a new project I am doing. I ended up cleaning up a lot of the expressions as I have a better understanding now (i am still no expert) but thanks to your great article that helped me out way back then. I wanted to just submit some better code especially for this example. As you noticed your code someone would have to alter depending on how many rows they have. Ideally we want code that looks up the rows automagically and does that for you. Which is what I am submitting for anyone else that could benefit from this.

// Parameters to set
csvLayer = thisComp.layer("Data-Driven-Sample.csv");  //point to layer with csv
toSortData = "Text Data";                             // Column Data you wish to sort - This data shows on this layer
sortByData = "Number Data";                           // Column data you wish to sort by

//code
let dualColumnData = [];
for(i=0;i<=csvLayer("Data")("Number of Rows")-1;i++){
   var obj = {};
   txt = csvLayer("Data")("Outline")(toSortData)(toSortData + " " + i).value;
   num = csvLayer("Data")("Outline")(sortByData)(sortByData + " " + i).value;
   obj['text'] = txt;
   obj['number'] = num;
   dualColumnData.push(obj);
} 
var list = []
list = dualColumnData.sort((a,b) => b.number-a.number).map((dualColumnData, number, array) => dualColumnData.text);
// if you wish to sort ascending rather then descending change 'b.number-a.number' to 'a.number-b.number' in the line above
 
list.join("\n")

Once again thanks so much for this article originally, I noticed its no longer up but you can find it on the internet archive, minus the sample file. But if anyone sees this I hopes it can save them some time.

@MarissaJ
Copy link
Author

Thanks for that, the code was written before the big Javascript update and I'm not super great at code lol. I appreciate you taking the time to write this out for anyone else interested in data-driven expressions!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment