I’m experimenting with the custom javascript flow node. I download data from Firebase and I want to sort by date. Here is the Javascript code that is not working:
const objs = inputs.input1; // bound to data variable 'dat'
objs.sort((a,b) => Date.parse(b.fields.date.stringValue) - Date.parse(a.fields.date.stringValue));
return [ 0, { result: objs } ];
I download the data collection from Firebase into the data variable dat
. So I bind inputs.input1
to dat
.
I output the sorted object (objs) on outputs.output1
. I set the output to a page variable with the same schema as the data so I can use it. I’ve looked at the output in the debugger and also examined a “stringified” version of the output and it appears to have the correct structure, but it is not sorted.
Here is the structure of the data that I download from Firebase:
[
{
"name": "projects/<projectID>/databases/(default)/documents/<collectionName>/<recordID>",
"fields": {
"total": {
"integerValue": "119"
},
"active": {
"booleanValue": true
},
"name": {
"stringValue": "Peter"
},
"date": {
"stringValue": "08-24-2022"
}
},
"createTime": "2022-08-24T20:44:19.770997Z",
"updateTime": "2022-08-25T14:45:24.994049Z"
}]
I’m pretty sure the javascript is “almost right” - but something is amiss. I’ve looked for answers in this forum, StackOverflow, and various javascript websites (like W3Schools).
I’d appreciate any insight into why this javascript does not sort my data.
========================= Edit ============================
I found the solution. I am not deleting this post because someone else might find it useful.
I discovered that the Date.parse() function only works reliably with ISO formatted date strings (mine are not - they are in the MM-DD-YYYY format). I found a function on StackOverflow that takes a date in virtually any format and converts it to an ISO formatted date. I then fed the result of that function to the sort routine and it works.