# Sort an array by character length.

## Recommended Posts

Has anyone tried an _ArraySortByCharLen()? I thought I had seen a topic on it before, but don't seem to be able to find it at the moment.

I'm going to try it myself, but thought it would be nice if someone had already done something like that before.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

##### Share on other sites

Has anyone tried an _ArraySortByCharLen()? I thought I had seen a topic on it before, but don't seem to be able to find it at the moment.

I'm going to try it myself, but thought it would be nice if someone had already done something like that before.

this is in scripts and scraps as of this morning,, but just incase someone checks here first, i'll put the code here too.

```#cs
Title:              _ArraySortByLength
Version:            v1.0
FileName:           ArraySortByLength.au3
Description:        Takes a single dimenstion array (with 0 being cound of elements, and returns the array sorted in ascending _
or descending order by length of each element
Author(s)           CameronsDad - Sean - AIM: SiCKPrLck
#ce
Func _ArraySortByLength(\$InArray,\$ascending = 1)
Dim \$TempArray[\$InArray[0]+1][2]
\$TempArray[0][0] = \$InArray[0]
For \$x = 1 to \$InArray[0]
\$TempArray[\$x][0] = \$InArray[\$x]
\$TempArray[\$x][1] = Number(StringLen(\$InArray[\$x]))
Next
Dim \$in,\$out
For \$out = 1 to \$TempArray[0][0]
\$temp = \$TempArray[\$out][0]
\$temp2 = Number(\$TempArray[\$out][1])
\$in = \$out
While \$in > 1 And Number(\$TempArray[\$in-1][1]) >= \$temp2
\$TempArray[\$in][0] = \$TempArray[\$in -1][0]
\$TempArray[\$in][1] = \$TempArray[\$in -1][1]
\$in = \$in -1
WEnd
\$TempArray[\$in][0] = \$temp
\$TempArray[\$in][1] = \$temp2
Next
For \$y = 1 to \$TempArray[0][0]
\$InArray[\$y] = \$TempArray[\$y][0]
Next
If Not \$ascending Then _ArrayReverse(\$InArray,1)
Return(\$InArray)
EndFunc```

## Create an account

Register a new account