# Help! Sorting Array =S

Go to solution Solved by oapjr,

## Recommended Posts

Hi!

I'm having a problem sorting array

if I have array like this:

1test10
test19
test15
1test13
1test1
test3
1test2
test7

using _ArraySort from Array.au3 the result is:

1test1
1test10
1test13
1test2
test15
test19
test3
test7

but what i really want is something like this:

1test1
1test2
1test10
1test13
test3
test7
test15
test19

Can someone help me?

Edited by oapjr
##### Share on other sites

• Moderators

A few people were messing around with logical sorting the other day.

I started to write a full udf for it, but got side tracked, I'm afraid I didn't get much past what they had done:

```Local \$aArr[] = ["1test10", "test19", "test15", "1test13", "1test1", "test3", "1test2", "test7"]
_ArraySortLogical(\$aArr, 0)
_ArrayDisplay(\$aArr)

Func _ArraySortLogical(ByRef \$avArray, \$iDescending = 0, \$iStart = 0, \$iEnd = 0, \$iSubItem = 0)

If Not IsArray(\$avArray) Then
Return SetError(1, 0, 0)
EndIf

Local \$iUB1 = UBound(\$avArray, 1), \$iUB2 = UBound(\$avArray, 2)

\$iDescending = ((\$iDescending = Default Or \$iDescending < 1 Or \$iDescending = False) ? 0 : 1)
\$iStart = ((IsKeyword(\$iStart) Or \$iStart < 1) ? 0 : \$iStart)
\$iEnd = ((IsKeyword(\$iEnd) Or \$iStart < 1 Or \$iEnd > (\$iUB1 - 1)) ? (\$iUB1-1) : \$iEnd)
\$iSubItem = ((IsKeyword(\$iSubItem) Or \$iSubItem < 1) ? 0 : \$iSubItem)

If \$iStart > \$iEnd Then
Return SetError(2, 0, 0)
EndIf

If \$iUB2 And \$iSubItem > (\$iUB2 - 1) Then
Return SetError (4, 0, 0)
EndIf

\$hDll = DllOpen("Shlwapi.dll")
If \$hDll = -1 Then
Return SetError(5, 0, 0)
EndIf

__ArraySortLogical1D(\$avArray, \$iDescending, \$iStart, \$iEnd, \$hDll)

DllClose(\$hDll)
EndFunc

Func __ArraySortLogical1D(ByRef \$avArray, Const ByRef \$iDesc, Const ByRef \$iStart, Const ByRef \$iEnd, Const ByRef \$hDll)

Local \$iRet, \$iLoop = 1, \$vTmp
Local \$iCompare = ((\$iDesc) ? 1 : -1)
While \$iLoop
\$iLoop = 0
For \$i = \$iStart To \$iEnd - 1
\$iRet = _WinAPI_StrCmpLogical(\$avArray[\$i], \$avArray[\$i + 1], \$hDll)
If \$iRet = 0 Then ContinueLoop
If \$iCompare = \$iRet Then ContinueLoop
\$vTmp = \$avArray[\$i + 1]
\$avArray[\$i + 1] = \$avArray[\$i]
\$avArray[\$i] = \$vTmp
\$iLoop = 1
Next
WEnd

EndFunc

Func _WinAPI_StrCmpLogical(\$sString1, \$sString2, \$hDll = Default)

\$hDll = ((IsKeyword(\$hDll) Or \$hDll < 1) ? \$hDll = "Shlwapi.dll" : \$hDll)

Local \$aRet = DllCall(\$hDll, "int", "StrCmpLogicalW", "wstr", \$sString1, "wstr", \$sString2)
If @error Then Return SetError(1, 0, -2)

Return \$aRet[0]
EndFunc```

It currently only works on 1D arrays.

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.

## Create an account

Register a new account

• ### Recently Browsing   0 members

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
×
• Create New...