0

I have defined the following function in the scripteditor

function SORTBYLEN(input) {
    return Array.isArray(input) ?
        input.sort((a,b) => a.length - b.length): input
}

I would expect to be able to give it a range of strings and it would return the range sorted by string length, yet when I try to use it, nothing changes.

enter image description here enter image description here

1 Answer 1

2

I thought that the reason of your issue is due to that input is 2 dimensional array. By this, the length of each element is 1. So the array of input is not sorted. In order to avoid this issue, how about the following modification?

Modified script:

return Array.isArray(input) ? input.sort((a, b) => a[0].length - b[0].length) : input;

or

return Array.isArray(input) ? input.flat().sort((a, b) => a.length - b.length) : input;

Reference:

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

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.