Help

Sort a list in scripting app

Solved
Jump to Solution
2953 8
cancel
Showing results for 
Search instead for 
Did you mean: 
Paul_Van_Dooren
6 - Interface Innovator
6 - Interface Innovator

Hello all,

I have a table that's filled with (linked) dates. To be able to view all the dates in a list, i've created in the script app a script to give me that list. But, is there a possibilty to sort that list?

Below the code:

 

// Change this name to use a different table
let table = base.getTable("Deadlines");
// Prompt the user to pick a record 
// If this script is run from a button field, this will use the button's record instead.
let record = await input.recordAsync('Select a record to use', table);
let Weer = record.getCellValue('NB-Weer - DATE');
let Dag = record.getCellValue('NB-Dagkrant - DATE');
let Pagina = record.getCellValue('NB-1-1 Pagina DATE');

output.markdown(`**NB-Weer:** ${Weer}`);
output.markdown(`**NB-Dagkrant:** ${Dag}`);
output.markdown(`**NB-Dagkrant:** ${Pagina}`);

 

... and the result:

Paul_Van_Dooren_0-1687346075504.png

Very much a scripting newbie so any help or pointers very much appreciated!

Allready a big thanks and any help is welcome

2 Solutions

Accepted Solutions

That should work

const table=base.getTable('Deadlines')
let record = await input.recordAsync('Select a record to use', table);
const flds=['NB-Weer - DATE','NB-Dagkrant - DATE','NB-1-1 Pagina DATE']
const getData=flds.map(f=>[record?.getCellValue(f),f]).sort().reverse().map(arr=>arr.reverse().join(': '))
output.table(getData)

See Solution in Thread

Stephen_Orr1
10 - Mercury
10 - Mercury

Hi @Paul_Van_Dooren,

Here's an easy to follow version

// Change this name to use a different table
let table = base.getTable('Deadlines');
// Prompt the user to pick a record
// If this script is run from a button field, this will use the button's record instead.
let record = await input.recordAsync('Select a record to use', table);
let Weer = record.getCellValue('NB-Weer - DATE') || '';
let Dag = record.getCellValue('NB-Dagkrant - DATE') || '';
let Pagina = record.getCellValue('NB-1-1 Pagina DATE') || '';

let dates = [
  { field: 'NB-Weer', value: Weer },
  { field: 'NB-Dagkrant', value: Dag },
  { field: 'NB-1-1 Pagina', value: Pagina },
];

//filter out empty dates (remove if you would prefer empty dates to remain)
dates = dates.filter((f) => f.value);

//sort array of field value objects
dates.sort((a, b) => String(a.value).localeCompare(String(b.value)));

for (let d of dates) {
  output.markdown(`**${d.field}** ${d.value}`);
}

-Stephen

 

 

See Solution in Thread

8 Replies 8
Alvaro_Hernande
6 - Interface Innovator
6 - Interface Innovator

I couldn´t test this solution inside Airtable, but in JS I would do this:

Just adding a sorting step after retrieving the values.

And at the end just iterate overthe sorted dates array and output each date.

Hope it works!

// Change this name to use a different table
let table = base.getTable("Deadlines");
// Prompt the user to pick a record 
// If this script is run from a button field, this will use the button's record instead.
let record = await input.recordAsync('Select a record to use', table);
let Weer = record.getCellValue('NB-Weer - DATE');
let Dag = record.getCellValue('NB-Dagkrant - DATE');
let Pagina = record.getCellValue('NB-1-1 Pagina DATE');

// Create an array to store the dates
let dates = [Weer, Dag, Pagina];

// Filter out empty dates
dates = dates.filter(date => date !== null);

// Sort the dates in descending order
dates.sort((a, b) => b - a);

// Format and output the sorted dates
output.markdown('**Sorted Dates (Newest First):**');
dates.forEach(date => {
  output.markdown(`- ${date}`);
});

 

Hi Alvaro,

Thank you for the quick response, unfortunately it didn't do the trick.
But you have given me an idea to group (array) the dates.

grtz,
Paul

That should work

const table=base.getTable('Deadlines')
let record = await input.recordAsync('Select a record to use', table);
const flds=['NB-Weer - DATE','NB-Dagkrant - DATE','NB-1-1 Pagina DATE']
const getData=flds.map(f=>[record?.getCellValue(f),f]).sort().reverse().map(arr=>arr.reverse().join(': '))
output.table(getData)
Stephen_Orr1
10 - Mercury
10 - Mercury

Hi @Paul_Van_Dooren,

Here's an easy to follow version

// Change this name to use a different table
let table = base.getTable('Deadlines');
// Prompt the user to pick a record
// If this script is run from a button field, this will use the button's record instead.
let record = await input.recordAsync('Select a record to use', table);
let Weer = record.getCellValue('NB-Weer - DATE') || '';
let Dag = record.getCellValue('NB-Dagkrant - DATE') || '';
let Pagina = record.getCellValue('NB-1-1 Pagina DATE') || '';

let dates = [
  { field: 'NB-Weer', value: Weer },
  { field: 'NB-Dagkrant', value: Dag },
  { field: 'NB-1-1 Pagina', value: Pagina },
];

//filter out empty dates (remove if you would prefer empty dates to remain)
dates = dates.filter((f) => f.value);

//sort array of field value objects
dates.sort((a, b) => String(a.value).localeCompare(String(b.value)));

for (let d of dates) {
  output.markdown(`**${d.field}** ${d.value}`);
}

-Stephen

 

 

Hey Stephen,

Thanks for your effort, unfortunately the sorting didn't work.
But you gave me an idea how to layout the final  view.

greetings,
Paul

Hello Alexey,

Thank you very much, this script did the trick!

greetings,
Paul

Oops, this is what I get for not testing 🙂 I updated my previous code to sort strings instead of numbers. By the way, I would highly recommend readable/well documented code over shorter code for the sake of future maintenance. If you've ever inherited a project where someone has tried to write things in the shortest way possible with zero commenting, you'll soon know pain. Good luck!

Hello Stephen,

You are right, the improvement you've made, made the script working. Thank you very much.
And yes, when you make comments in a script it is a big help for future maintenance!