Jump to content

Ascending sort of number sequence


Recommended Posts

  • Developers

Here you have something to study and play with:

#include<Array.au3>
$str = "18,03,48,23"
$aStr = StringSplit($str,",",2)
_ArraySort($aStr)
for $x = 0 to UBound($aStr)-1
    MsgBox(0,"value " & $x ,$aStr[$x])
Next

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

A purely string, no arrays sort method.
This _StringSortRE() function bubble sorts the separated data within the string by a specified sort order (ascending or descending) and a specified data type (numerical or alphabetical).

Local $sStr = "12" & @CRLF & "34" & @CRLF & "56" & @CRLF & "7" & @CRLF & "89" & @CRLF & "10" & _
        @CRLF & "11" & @CRLF & "13" & @CRLF & "14" & @CRLF & "15" & @CRLF & "16" & @CRLF & "99" ; & @CRLF

MsgBox(0, 'StringSortRE ', "Original: " & @CRLF & $sStr & @CRLF & @CRLF & "_StringSortRE($sStr) Ascending Alpha - Default: " & @CRLF & _StringSortRE($sStr))
MsgBox(0, 'StringSortRE ', "Original: " & @CRLF & $sStr & @CRLF & @CRLF & "_StringSortRE($sStr, 0, 1) Ascending Numeric: " & @CRLF & _StringSortRE($sStr, 0, 1))
MsgBox(0, 'StringSortRE ', "Original: " & @CRLF & $sStr & @CRLF & @CRLF & "_StringSortRE($sStr, 1, 1) Descending Numeric: " & @CRLF & _StringSortRE($sStr, 1, 1))
MsgBox(0, 'StringSortRE ', "Original: " & @CRLF & $sStr & @CRLF & @CRLF & "_StringSortRE($sStr, 1, 0) Descending Alpha: " & @CRLF & _StringSortRE($sStr, 1, 0))

Local $str = "18,3,48,23"

MsgBox(0, 'StringSortRE ', "Original: " & @CRLF & $str & @CRLF & @CRLF & "_StringSortRE($sStr) Ascending Alpha - Default: " & @CRLF & _StringSortRE($str, 0, 0, ","))
MsgBox(0, 'StringSortRE ', "Original: " & @CRLF & $str & @CRLF & @CRLF & "_StringSortRE($sStr, 0, 1) Ascending Numeric: " & @CRLF & _StringSortRE($str, 0, 1, ","))
MsgBox(0, 'StringSortRE ', "Original: " & @CRLF & $str & @CRLF & @CRLF & "_StringSortRE($sStr, 1, 1) Descending Numeric: " & @CRLF & _StringSortRE($str, 1, 1, ","))
MsgBox(0, 'StringSortRE ', "Original: " & @CRLF & $str & @CRLF & @CRLF & "_StringSortRE($sStr, 1, 0) Descending Alpha: " & @CRLF & _StringSortRE($str, 1, 0, ","))


;For descending order, set $iDescending to a numeric value not zero.
;To sort numbers, set $iNumbers to a numeric value not zero. (zero is False, not zero is True)
;
Func _StringSortRE($sString, $iDescending = 0, $iNumbers = 0, $d = @CRLF) ; ($d = Delimiters)
    Local $iCount, $bPass = False
    StringRegExpReplace($sString, "(" & $d & ")", " ")
    $iCount = @extended + 1 - StringRegExp($sString, "(" & $d & ")$") ; Trailing delimiter is ignored if present.
    While Not $bPass
        $bPass = True
        For $x = 0 To $iCount - 2
            $sItem1 = StringRegExpReplace($sString, "(?s)(([^" & $d & "]+" & $d & "){" & $x & "})([^" & $d & "]+)(.*)", "${3}") ; Get current $x item
            $sItem2 = StringRegExpReplace($sString, "(?s)(([^" & $d & "]+" & $d & "){" & $x & "})([^" & $d & "]+)(" & $d & ")([^" & $d & "]+)(.*)", "${5}") ; Get the next item after the current $x item
            If ($iDescending = 0 And $iNumbers <> 0 And Number($sItem1) > Number($sItem2)) Or _       ; (Ascending, Numeric)  (0,1)
                    ($iDescending = 0 And $iNumbers = 0 And $sItem1 > $sItem2) Or _                   ; (Ascending, Alpha)    (0,0)
                    ($iDescending <> 0 And $iNumbers <> 0 And Number($sItem1) < Number($sItem2)) Or _ ; (Descending, Numeric) (1,1)
                    ($iDescending <> 0 And $iNumbers = 0 And $sItem1 < $sItem2) Then                  ; (Descending, Alpha)   (1,0)
                $sString = StringRegExpReplace($sString, "(?s)(([^" & $d & "]+" & $d & "){" & $x & "})([^" & $d & "]+)(" & $d & ")([^" & $d & "]+)(" & $d & ")?", "${1}${5}${4}${3}${6}") ; Swap $sItem1 and $sItem2 i.e. swap back-reference ${3} and ${5}.
                $bPass = False
            EndIf
        Next
    WEnd
    Return $sString
EndFunc   ;==>_StringSortRE

 

Link to comment
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
 Share

×
×
  • Create New...