I'm quite new to Arcade scripting and need some assistance, I have developed a Field Maps form to evaluate site suitability for tree cultivars. The trees have suitability scores 0-4 for a range of site characteristics. The scores are then totalled. I want to sort these scores descending and return the top 5 names and scores as a readable list.
I typed this query into Copilot along with the attached subset of my 50 tree cultivars and example scores, and received the following (also in an attached file), which does not work.
// Create an array of field names with their scores
var trees = [
{ "name": "Weraiti", "score": $feature.Weraiti },
{ "name": "Yeogi", "score": $feature.Yeogi },
{ "name": "Yunnan", "score": $feature.Yunnan },
{ "name": "Booth", "score": $feature.Booth },
{ "name": "Awanui", "score": $feature.Awanui },
{ "name": "Aegyptica", "score": $feature.Aegyptica },
{ "name": "Gigantea", "score": $feature.Gigantea },
{ "name": "Glenmark", "score": $feature.Glenmark }
];
// Sort the array by scores in descending order
var sortedTrees = Sort(trees, function(item1, item2) {
return item2.score - item1.score;
});
// Get the top 5 items
var topTrees = Slice(sortedTrees, 0, 5);
// Create a readable list
var readableList = "";
for (var i = 0; i < Count(topTrees); i++) {
readableList += (i + 1) + ". " + topTrees[i].name + " - " + topTrees[i].score + TextFormatting.NewLine;
}
return readableList;
And when pasted into Arcade Playground shows where the error is.

My deadline for this app is fast approaching so I would appreciate a reworked expression that I can apply to my complete tree array.
Many thanks