aws / aws/aws-appsync-community

Array.prototype.sort() with a compare function throwing errors

Open
#325 5 comments 1 reaction 0 assignees View on GitHub
feature-request
Dominant language
HTML
Stars
507
Forks
37
PR merge metrics
No merged PRs in 30d

Description

I have a list of objects that I need to sort, for example
```javascript
const scores = [
{ name: "Alice", score: 85 },
{ name: "Bob", score: 92 },
{ name: "Charlie", score: 78 },
{ name: "David", score: 95 },
];

// Sort the scores array by the "score" property in ascending order
scores.sort((a, b) => a.score - b.score);
```

However, AppSync errors out when I try to save my code
```
@aws-appsync/no-function-passing: Functions can not be passed as an argument
```

This is problematic and doesn't make a ton of sense because 1) passing functions as arguments to other stuff like `Array.prototype.reduce` works fine, and 2) VTL had a sort function that could sort objects in a list by an attribute. I'm trying to rewrite all my functions in Javascript but there are some that I've had to continue using VTL for and others where I've had to find ugly creative workarounds to get sorting to work.

Contributor guide

Open the contributing guide

Research direction

Start with the JavaScript example and the reported @aws-appsync/no-function-passing error, then determine whether comparator functions are supported by the AppSync JavaScript runtime. Done means Array.prototype.sort accepts a comparator for object arrays without that error, with behavior documented or demonstrated for the example.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, javascript
Domain
api, backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.