Jump to content
Sign in to follow this  
dcop

_arraysort and various lengths of numbers

Recommended Posts

dcop

[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 this post


Link to post
Share on other sites
water

In short: Yes


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
Malkey

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

Share this post


Link to post
Share on other sites
dcop

Thanx Malkey :D

Share this post


Link to post
Share on other sites

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
Sign in to follow this  

×