AppSync JavaScript Support for Array.sort of objects

0

While implementing JS resolvers in AppSync I can't find a way to sort an Array of objects.

array_of_objects.sort((a, b) => (a.event_ts - b.event_ts))

is failing with: "errorType": "UNSUPPORTED_SYNTAX_TYPE",

"value": "Unsupported Syntax Type: ArrowFunction",

const compareFn = (a, b) => {
        return (a.event_ts - b.event_ts)
        if ( a.event_ts < b.event_ts ){
            return -1;
          }
          if ( a.event_ts > b.event_ts ){
            return 1;
          }
          return 0;
    };

array_of_objects.sort(compareFn)

Doesn't sort the array in place and doesn't return a sorted array (JavaScript is annoying...).

The documentation of AppSync (https://docs.aws.amazon.com/appsync/latest/devguide/built-in-objects-functions.html) mentions that Array.prototype.sort() is supported, and indeed, if I pass a simple array (of Strings, for example) it is working OK. However, for objects, I can't get it to work.

MLGuy
asked a year ago389 views
1 Answer
0

Hi, I would suggest to try this out:

  • try, just for experiment, to not use "=>" arrow function but rather a function
const compareFn = function(a, b) {
        return (a.event_ts - b.event_ts)
        if ( a.event_ts < b.event_ts ){
            return -1;
          }
          if ( a.event_ts > b.event_ts ){
            return 1;
          }
          return 0;
    };
profile picture
EXPERT
answered a year ago
  • Thanks for your attention @alatech. However, AppSync doesn't support functions as arguments, and when I try your suggestion I get the following error:

     "errorType": "UNSUPPORTED_SYNTAX_TYPE",
     "value": "Unsupported Syntax Type: FunctionExpression",
    

    The might be a bug in the implementation of AppSync or any other limitation that I need to walk around.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions