We do our best to ensure that the code in these articles works as advertised, but please be aware that Zapier Support does not officially help with code steps due to their advanced nature. Feel free to comment on the article or ask in the Zapier Community, where we have code-savvy folks (including the author of this post).
Q: Ever wanted to sort to items in an array?
A: Use a Code app as a Zap step: https://zapier.com/apps/code/help
Code step configuration using JavaScript
NOTE: Map your line items array to the right side value for the “Set” Input Data variable in place of “A,B,C,C,B,A”.
Sort items in array in ASCENDING order
COPY THE CODE
let Set = inputData.Set.split(","); // creates array by splitting Input Data Variable at commas
Set.sort(); // sorts array in alphabetical and ascending order
output = r{Set}];
Sort items in array in DESCENDING order
COPY THE CODE
let Set = inputData.Set.split(","); // creates array by splitting Input Data Variable at commas
Set.sort().reverse(); // reverses the sort order of the array items
output = <{Set}];
Results
Code step results to sort array items in ASCENDING order.
Code step results to sort array items in DESCENDING order.
Contribution by Troy Tessalone
Troy is a Certified Zapier Expert who automates workflows with no-code and low-code apps to help clients save time and make money.