I have an array like this:
arr = []
arr[0] = "ab"
arr[1] = "abcdefgh"
arr[2] = "abcd"
After sorting, the output array should be:
arr[0] = "abcdefgh"
arr[1] = "abcd"
arr[2] = "ab"
I want in the descending order of the length of each element.
You can use
Array.sortmethod to sort the array. The callback function should use the length of item as the sorting criteria:You can specify additional criteria if length of two items are same: