Skip to main content
Best answer

How to sort a large array of line items (with Zapier Storage + Code?)


Mutiny Ops

Hello Zapmates!

Here’s the challenge…

We’re trying to sort content to recommend to a person using scores. I want to return the URL slugs (that present the slug) in order of the highest score for each contact_id 

 

I’ve got a Loop in my Zap to fetch the relevant scores for each Slug line item. I’m not sure how best to push this to Storage with one key (the contact_id), multiple slug/scores.

 

e.g. 

 

contact_id: abcde

 

Slug: example-slug-a

Score: 56

 

Slug: example-slug-b

Score: 85

 

Slug: example-slug-c

Score: 74

 

I’m then not sure how to get all these entries looped through for each contact (there could be 20-30) and then sort them. There’s an outline here, but it uses alphabetical sorting vs. the idea of a score.

Maybe there’s a hacky way to preprend the score value to slug name then use the same desc logic?

Any help would be really appreciated

Best answer by Mutiny OpsBest answer by Mutiny Ops

We resolve this by asking ChatGPT + the Zapier docs. It should be much easier to handle arrays!

  1. Create a temporary delimiter between values
  2. Converting the input (Zapier Code data input is always a string, so need to convert to an array)
  3. Run a comparison function of unique values
  4. Write back values to map into our onward tool.
let stringSet = inputData.Playbooks;

console.log(stringSet) // Show the ugly string.

let Set = stringSet.split('|||'); // Split out by the temporary delimiter

console.log(Set); // Show the array that's fetched

console.log(Array.isArray(Set)) // Evaluates to true

// Suspect each line is one long string still.
console.log(Number.isInteger(Set.score)); // Evaluates to false

// JSON.parse() was causing problems. GPT alternative suggestion to turn string into an array.
let parsedSet = Set.map(str => {
  let obj = {};
  str = str.replace(/,$/, '');
  let keyValuePairs = str.match(/(\w+):("[^"]+"|\w+)/g);
  keyValuePairs.forEach(pair => {
    let [key, value] = pair.split(':');
    key = key.trim();
    value = value.trim();
    obj[key] = value.match(/^".*"$/) ? value.slice(1, -1) : Number(value);
  });
  return obj;
});

console.log(parsedSet);


// Make sure we're only evaluating unique slugs to avoid duplicate entries
let uniqueSet = [];
let slugMap = {};

for (let item of parsedSet) {
    if (!slugMap[item.slug]) {
        slugMap[item.slug] = true;
        uniqueSet.push(item);
    }
}

let sortedSet = uniqueSet.sort((a, b) => b.score - a.score); // Sort the unique set by score

console.log(sortedSet); // Show the output of the whole set.

let sortedSlugs = sortedSet.map(item => item.slug);

console.log(sortedSlugs);

output = { sortedSlugs, sortedSet }; // Write the output back to the Zap

 

View original
Did this topic help you find an answer to your question?
This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

2 replies

Troy Tessalone
Forum|alt.badge.img+14

Hi @Mutiny Ops 

Good question.

For more context, please post detailed full screenshots with how your Zap steps are currently configured.


Mutiny Ops
  • Author
  • Beginner
  • 1 reply
  • Answer
  • May 31, 2023

We resolve this by asking ChatGPT + the Zapier docs. It should be much easier to handle arrays!

  1. Create a temporary delimiter between values
  2. Converting the input (Zapier Code data input is always a string, so need to convert to an array)
  3. Run a comparison function of unique values
  4. Write back values to map into our onward tool.
let stringSet = inputData.Playbooks;

console.log(stringSet) // Show the ugly string.

let Set = stringSet.split('|||'); // Split out by the temporary delimiter

console.log(Set); // Show the array that's fetched

console.log(Array.isArray(Set)) // Evaluates to true

// Suspect each line is one long string still.
console.log(Number.isInteger(Set.score)); // Evaluates to false

// JSON.parse() was causing problems. GPT alternative suggestion to turn string into an array.
let parsedSet = Set.map(str => {
  let obj = {};
  str = str.replace(/,$/, '');
  let keyValuePairs = str.match(/(\w+):("[^"]+"|\w+)/g);
  keyValuePairs.forEach(pair => {
    let [key, value] = pair.split(':');
    key = key.trim();
    value = value.trim();
    obj[key] = value.match(/^".*"$/) ? value.slice(1, -1) : Number(value);
  });
  return obj;
});

console.log(parsedSet);


// Make sure we're only evaluating unique slugs to avoid duplicate entries
let uniqueSet = [];
let slugMap = {};

for (let item of parsedSet) {
    if (!slugMap[item.slug]) {
        slugMap[item.slug] = true;
        uniqueSet.push(item);
    }
}

let sortedSet = uniqueSet.sort((a, b) => b.score - a.score); // Sort the unique set by score

console.log(sortedSet); // Show the output of the whole set.

let sortedSlugs = sortedSet.map(item => item.slug);

console.log(sortedSlugs);

output = { sortedSlugs, sortedSet }; // Write the output back to the Zap