# _ArraySort Question

## Recommended Posts

I would like to sort array that contain number and dot as a key, but to get it sorted correctly, I need to delete the dot between the numbers.

eg:

data is 1.500, 500 and 11.500

Without delete the dot, using _ArraySort will result:

1.500, 11.500 and 500 which is understandable.

To get it sorted correctly, I delete the dot and convert it to number using number().

Code is below. It's working perfect. I wonder do we really need such a long code just for this? ```#include <array.au3>

Local \$aArray = [['1','Michael','Mama ','1.500'], ['5','Susan','Xray ','500'], ['2', 'Adele', 'Telly ', '11.500']]

; remove the "." so it can sort correctly
For \$a = 0 To UBound(\$aArray) - 1
\$aArray[\$a] = Number(StringReplace(\$aArray[\$a], ".", ""))
Next

_ArraySort(\$aArray, 0, 0, 0, 3)

For \$a = 0 To UBound(\$aArray) - 1
\$aArray[\$a] = NumberCommas_with_dot(\$aArray[\$a])
Next

_arraydisplay(\$aArray)

Func NumberCommas_with_dot(\$Number)
;Given a number such as 12345678 will return 12,345,678
Dim \$Result='', \$Pos
If StringIsDigit(\$Number) Then
\$Pos = StringLen(\$Number)-2
While \$Pos > -2
If \$Pos < 1 Then
\$Result = StringMid(\$Number, 1, \$Pos + 2) & '.' & \$Result
Else
\$Result = StringMid(\$Number, \$Pos, 3) & '.' & \$Result
EndIf
\$Pos = \$Pos - 3
WEnd
\$Result=StringLeft(\$Result, StringLen(\$Result) - 1)
EndIf
Return \$Result
EndFunc```
##### Share on other sites

Shortened code:

```#include <array.au3>

Local \$aArray = [['1','Michael','Mama ','1.500'], ['5','Susan','Xray ','500'], ['2', 'Adele', 'Telly ', '11.500']]

; remove the "." so it can sort correctly
For \$a = 0 To UBound(\$aArray) - 1
\$aArray[\$a] = Number(StringReplace(\$aArray[\$a], ".", ""))
Next

_ArraySort(\$aArray, 0, 0, 0, 3)

For \$a = 0 To UBound(\$aArray) - 1
\$aArray[\$a] = StringRegExpReplace(\$aArray[\$a], "(?<=\d)(\d\d\d)(?=(?:\d\d\d)*\$)", ",\$1")
Next

_arraydisplay(\$aArray)```

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

##### Share on other sites

Shortened code:

```#include <array.au3>

Local \$aArray = [['1','Michael','Mama ','1.500'], ['5','Susan','Xray ','500'], ['2', 'Adele', 'Telly ', '11.500']]

; remove the "." so it can sort correctly
For \$a = 0 To UBound(\$aArray) - 1
\$aArray[\$a] = Number(StringReplace(\$aArray[\$a], ".", ""))
Next

_ArraySort(\$aArray, 0, 0, 0, 3)

For \$a = 0 To UBound(\$aArray) - 1
\$aArray[\$a] = StringRegExpReplace(\$aArray[\$a], "(?<=\d)(\d\d\d)(?=(?:\d\d\d)*\$)", ",\$1")
Next

_arraydisplay(\$aArray)```

Wow, again the power of RegExp Thanks, jchd Anyone? ## Create an account

Register a new account

• ### Recently Browsing   0 members

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
×
• Create New...