2

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

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.

Trying an inline arrow function:

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

fails with

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

Trying an external arrow function:

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)

It doesn't sort the array in place or return a sorted array.

Trying a function argument:

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;
    };

array_of_objects.sort(compareFn)

Fails with:

"errorType": "UNSUPPORTED_SYNTAX_TYPE"
"value": "Unsupported Syntax Type: FunctionExpression"
5
  • What about a plain function definition statement? Commented Feb 4, 2023 at 23:58
  • Can you give an example? Commented Feb 5, 2023 at 5:11
  • function compareFn(a, b) { ... } Commented Feb 5, 2023 at 13:34
  • I get Unsupported Syntax Type: FunctionExpression error (see in the question details). Commented Feb 5, 2023 at 14:29
  • Well a function declaration statement is not a "function expression", so I can't say what that parser is doing. A function declaration statement starts with the keyword function, while a function expression does not. Commented Feb 5, 2023 at 14:36

3 Answers 3

3

At time of writing this answer, the documentation also states that:

Note

Array.prototype.sort() doesn't support arguments.

So you are basically stuck with the default array sort, which sorts in place in ascending order :/

A bit disappointing isn't it?

I'm afraid your best guess would be to implement your own sorting function.

Sign up to request clarification or add additional context in comments.

Comments

0

This works at the time of writing.

Since Array.sort doesnt support anonymous functions in AppSync_JS, you need to defined your sorting function first.

const sorter = (a: item, b: item ) => b.event_ts - a.event_ts

array_of_objects.sort(sorter); 

Comments

0

A quick n dirty script that can be used in aws appsync resolver. Takes care of sort, parameterised func, inline func, for & ++ constraints of appsync/js - hope it helps someone

const cfunc_=(a,b)=>a>b
function bs_(arr) {
    var i=0, j,tmp1,tmp2,tmpArr=arr.slice(),sliceArr,break_=false,len = arr.length,isSwapped = false;
    tmpArr.forEach(
        (ele)=>{
            isSwapped = false;
            sliceArr=arr.slice(0,len-i-1)
            j=0;
            sliceArr.forEach(
                (tmpEle)=>{
                  if (cfunc_(arr[j],arr[j + 1])) {
                      tmp2 = arr[j]
                      arr[j] = arr[j + 1];
                      arr[j + 1] = tmp2;
                      isSwapped = true;
                  }
                  j=j+1
               }
            )
            i=i+1
            if(!isSwapped)
                return
        }
    )
    console.log(arr)
}
var arr_=[2,5,2,8,4,7,2,9]
bs_(arr_)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.