Skip to main content

📌  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.

Thank you for that @Troy Tessalone . I do not understand how to use the result that goes into the Javascript variable”output”, in my Zap further steps.

 


Hi @Patrick.emin 

You’ll want to add the Code step after you’ve released the existing digest that will return all the items in the digest.

 


Thanks but I think it’s what I am doing, See step 3 of my screen capture, my digest is released on a weekly basis. On step 5 I can access my digest and process it with the Code module but the problem is after that treatment, I am unable to find where the result is, I guess it is the content of the Javascript variable “output”, but how do I get that content ?


@Patrick.emin 

Depends on how the Code is configured.

In the example, the output data point would be Set.

 


Yes I see that, so in that case, would you be kind enough to explain me how for example I can send me an email with the content of that variable using a Zapier step? (I am really stuck on that and probably not far from succeeding...) 😀


@Patrick.emin 

You’d map the desired Code step output variable to the body of the email using the ‘custom’ option as indicated here: https://zapier.com/help/create/basics/set-up-your-zap-action#set-up-your-action


Thank you very much for your answers I did not succeed I do not understand what to do and I have probably reached my level of competency in this area! But I don't want to abuse your goodwill and patience. So I will wait for Zapier to offer us the option of sorting a Digest which I believe is in the plans.


@Patrick.emin 

I would not wait around for a feature to be released.

It may never be released, it could be released in years, it could be released soon (unlikely).

Zapier does not provide timelines on when features will be released.

 

Might be best to hire a Zapier Expert to help: https://zapier.com/experts


Yes, thanks for the advice.


Possible to sort by whole number?

 

Hi,

 

I’m using this code to sort some data that begins with numbers such as 5, 20, 50, 150, 200, 250, etc.

 

It currently would put the above in this order, based on the first number alone: 150, 20, 200, 250, 5, 50

 

Is there anyway to get it to sort based on the whole number?

 

Thanks,

 

Jon


Hi @Jon Hall 

INPUT

Array of numbers

 

CODE

Javascript

let X = inputData.X.split(",");

X.sort(function(a, b) {
return a - b;
});

output = ={X}];

 

 

OUTPUT

Sorted array of numbers

 

 


Reply