# Sort array by length

## Recommended Posts

i made this for a friend, he suggested posting it here for others.... here ya go.

```#include<array.au3>
#cs
Title:              _ArraySortByLength
Version:            v2.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 \$inner,\$outer,\$temp,\$temp2
\$n = \$temparray[0][0]
\$h = 1
While \$h <= \$n/3
\$h = \$h * 3 + 1
WEnd

While \$h >0
For \$outer = \$h To \$n
\$temp = \$temparray[\$outer][0]
\$temp2 = Number(StringStripWS(\$temparray[\$outer][1],1))
\$inner = \$outer
While \$inner > (\$h - 1) And Number(StringStripWS(\$temparray[\$inner-\$h][1],1)) >= Number(\$temp2)
\$temparray[\$inner][0] = \$temparray[\$inner-\$h][0]
\$temparray[\$inner][1] = \$temparray[\$inner-\$h][1]
\$inner = \$inner - \$h
WEnd
\$temparray[\$inner][0] = \$temp
\$temparray[\$inner][1] = \$temp2
Next
\$h = (\$h - 1)/3
WEnd
For \$y = 1 to \$TempArray[0][0]
\$InArray[\$y] = \$TempArray[\$y][0]
Next
If Not \$ascending Then _ArrayReverse(\$InArray,1)
Return(\$InArray)
EndFunc```

***edit*** updated to 2.0, changed the sort algo for a faster one, this one sorts the same data in about 10% of the time required of the original. The old one was still pretty fast for most arrays, but with hundreds of elements, it started slowing down. Using an array of 1000 elements, this sort was on average 10 times faster.

##### Share on other sites

i made this for a friend, he suggested posting it here for others.... here ya go.

And you gave it the the option for decending or not too!

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

And you gave it the the option for decending or not too!

i was going to make it for multi-dimension arrays also, but then i started thinking of how many times i've ever had to sort an array, single or multi dimentional, by element length, and this morning was the first time EVER... If there's a big demand for this to be multi-dimensional i'll add it in, but i think you're the only one that'll ever use it

##### Share on other sites

I know this is an old thread but I was wondering if you had any samples for this code. My test did not turn out well so I probably use it for something it is not supposed to or?

```Func test_ArraySortByLength()
Local \$a = _ArrayCreate("aa","aab","abc","ab","acde","abc")
Local \$a2
\$a2 = _ArraySortByLength(\$a)

Local \$i
For \$i = 0 to UBound(\$a2) -1
ConsoleWrite(\$a2[\$i] & @LF)
Next
ConsoleWrite(  @LF)
\$a2 = _ArraySortByLength(\$a, 0)
For \$i = 0 to UBound(\$a2) -1
ConsoleWrite(\$a2[\$i] & @LF)
Next
EndFunc```

Gives:

```aa
aab
abc
ab
acde
abc

aa
abc
acde
ab
abc
aab```

## Create an account

Register a new account