Jump to content

# _arraysort and various lengths of numbers

## Recommended Posts

[0]|975 Team: GP Racing Season Total =

[1]|881 Team: HD Lite Season Total =

[2]|1147 Team: MCR Season Total =

[3]|1066 Team: JJ McClure Season Total =

[4]|1056 Team: DJ Motorsports Season Total =

[5]|1056 Team: Big Daddy Irv Season Total =

The above array is after it sorted, desending. Because 9 and 8 are higher than the 1's they end up on top. Do I need to parse off the numbers, clean the whites space, sort the array with just the numbers and put back together again?

##### Share on other sites

In short: Yes

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

##### Share on other sites

This example could be labeled a custom designed array sort function.

Using StringRegExpReplace, the part to be sorted on is specifically pick out of each element of the array for comparison. The elements are then swapped if required.

Theoretically, to customize for another array, it is just a matter of rewriting the regular expressions to get the parts of the array that need sorting on.

```#include <Array.au3>

Local \$aArray[6]
\$aArray[0] = "|975 Team: GP Racing Season Total = "
\$aArray[1] = "|881 Team: HD Lite Season Total = "
\$aArray[2] = "|1147 Team: MCR Season Total = "
\$aArray[3] = "|1066 Team: JJ McClure Season Total = "
\$aArray[4] = "|1056 Team: DJ Motorsports Season Total = "
\$aArray[5] = "|1056 Team: Big Daddy Irv Season Total = "

_ArraySortCust(\$aArray)
_ArrayDisplay(\$aArray)

Func _ArraySortCust(ByRef \$aArr)
Local \$iFlag = 1, \$sTemp, \$iPrev, \$iThis
While \$iFlag = 1
\$iFlag = 0
For \$i = 1 To UBound(\$aArr) - 1
\$iPrev = Number(StringRegExpReplace(\$aArr[\$i - 1], "\|(\d+) .*", "\1"))
\$iThis = Number(StringRegExpReplace(\$aArr[\$i], "\|(\d+) .*", "\1"))
; ==== 1st sort ========
;ConsoleWrite(\$iPrev & " " & \$iThis & @CRLF)
; Sort highest number to lowest number order.
If \$iPrev < \$iThis Then
\$iFlag = 1 ; Cause While loop to loop one more time.
\$sTemp = \$aArr[\$i - 1] ; Swap routine
\$aArr[\$i - 1] = \$aArr[\$i]
\$aArr[\$i] = \$sTemp
EndIf
; ======2nd sort ========
;ConsoleWrite(StringRegExpReplace(\$aArr[\$i-1],"\|(\d+) (.*)", "\2") & " " & StringRegExpReplace(\$aArr[\$i],"\|(\d+) (.*)", "\2") & @CRLF)
; If numbers are equal then sort on what follows the numbers A to Z order.
If (\$iPrev = \$iThis) And (StringRegExpReplace(\$aArr[\$i - 1], "\|(\d+) (.*)", "\2") > StringRegExpReplace(\$aArr[\$i], "\|(\d+) (.*)", "\2")) Then
\$iFlag = 1
\$sTemp = \$aArr[\$i - 1] ; Swap routine
\$aArr[\$i - 1] = \$aArr[\$i]
\$aArr[\$i] = \$sTemp
EndIf
Next
;ConsoleWrite("\$iFlag = " & \$iFlag & @CRLF & @CRLF)
WEnd
EndFunc ;==>_ArraySortCust```

Thanx Malkey

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

## Sign in

Already have an account? Sign in here.

Sign In Now
• ### Recently Browsing   0 members

• No registered users viewing this page.
×

• Wiki

• Back

• #### Beta

• Git
• FAQ
×
• Create New...