Jump to content
Sign in to follow this  
liagason

Ascending sort of number sequence

Recommended Posts

Hello everyone,

How can I display in ascending  sequence some numbers stored in a string variable?

$str = "18,03,48,23"

MsgBox(0,"test",$str)

I would like it to display "03,18,23,48"

Share this post


Link to post
Share on other sites

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.
  :)

Share this post


Link to post
Share on other sites
4 hours ago, liagason said:

... I would like it to display "03,18,23,48"

also take a look at the _ArrayToString() function...


small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
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

 

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  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By nooneclose
      I need to dynamically resize my 2d array while looping. 
      I know this code:
      ReDim $rArray[UBound($rArray) + 1] works for the rows, however, I also need to increase the columns. How would i go about increasing both rows and columns while looping? 
    • By Colduction
      Hi guys!, i have a problem to convert Python code to AutoIt code, in fact i had not coded with Python yet!, this code is about permutation a string's case, i will be happy with your comments :)❤;

      Python code:
       
      # Python code to print all permutations # with respect to cases # Function to generate permutations def permute(inp): n = len(inp) # Number of permutations is 2^n mx = 1 << n # Converting string to lower case inp = inp.lower() # Using all subsequences and permuting them for i in range(mx): # If j-th bit is set, we convert it to upper case combination = [k for k in inp] for j in range(n): if (((i >> j) & 1) == 1): combination[j] = inp[j].upper() temp = "" # Printing current combination for i in combination: temp += i print(temp), # Driver code permute("Hello") # This code is contributed by Sachin Bisht
      My code in AutoIt:
      ; https://www.geeksforgeeks.org/permute-string-changing-case/ _PermuteCase("ABC") Func _PermuteCase($sText) If StringRegExp($sText, "^[A-Za-z]{1,}$") Then Local $iLength = StringLen($sText) ; Get length of the text. Local $iMaxPerm = 2 ^ $iLength ; Number of permutations is 2^n Local $sLow_Text = StringLower($sText) ; Converting string to lower case Local $asChrs = StringToASCIIArray($sLow_Text) ; Split the text into array of chars. For $i = 1 To $iMaxPerm Step 1 For $j = 0 To $asChrs[0] ;................................................... Next Next Else Return SetError(-1, 0, "Error: Input is incorrect!") EndIf EndFunc ;==>_PermuteCase  
       
       
       
       
       
      ====================== SOLUTION by @TheXman ======================
       
    • By careca
      This is another take on string triggers, triggers on specific strings.
      Able to simple text pasting,
      opening links (as long as there's a www. http:\\ or https:\\ at the beggining)
      and is able to open applications, if there is a parameter in the parameter field, it uses it.
      Shows your external, lan, and gateway ip's.
      Able to change system volume by a set percentage, reading from the inputbox the number the user sets, if 0 or empty uses system default.
      The 4 longer input fields at the bottom, represent the process scaning, the 2 to the left continuously scan for the process name,
      and the 2 to the right are executed when the process in the left is found.
      process 1 for app 1, example, look for notepad.exe and start paint.exe if notepad is found.
      I made this because the existing string trigger applications didn't do it for me.
      I did this for me, but if someone finds it useful all the better.
    • By Zaoka
      Hi guys
      Need little help with filtering.
      I'm trying to filter specific weeks from power pivot table using this code recorded with Excel VBA :
       
      #include <Excel.au3> Global $oExcel = _Excel_Open() Global $oWorkbook = _Excel_BookOpen($oExcel, "C:\Users\....\Orders.xlsb") $oWorkbook.PivotTables("PivotTable1").PivotFields( _ "[Report 2].[Week].[Week]").VisibleItemsList = Array( _ "[Report 2].[Week].&[10]", "[Report 2].[Week].&[11]", _ "[Report 2].[Week].&[12]", "[Report 2].[Week].&[13]", _ "[Report 2].[Week].&[14]", "[Report 2].[Week].&[15]") But get error
      error: Array(): undefined function. Not sure how to resolve this.
    • By LxP
      I'm trying to pass a nested array to a function, such that the function alters the inner array.
      I was surprised to find that this minimal reproducible example, despite its use of ByRef, seems to pass a copy of the inner array to the function:
      #include <Array.au3> ; a boring old array Local $aInnerArray[5] = [1, 2, 3, 4, 5] ; a one-element array containing a reference to the other array Local $aOuterArray[1] = [$aInnerArray] ; intention: take a nested array and alter its inner array ; reality: the inner array seems to be getting copied Func ChangeIt(ByRef $aOuter) Local $aInner = $aOuter[0] $aInner[2] = 0 EndFunc ; Expected: [1, 2, 3, 4, 5] ; Actual: [1, 2, 3, 4, 5] ✔ _ArrayDisplay($aInnerArray, 'Before') ; $aOuterArray passed by-ref, should receive reference to $aInnerArray ; Therefore should change $aInnerArray to [1, 2, 0, 4, 5] ChangeIt($aOuterArray) ; Expected: [1, 2, 0, 4, 5] ; Actual: [1, 2, 3, 4, 5] ✘ _ArrayDisplay($aInnerArray, 'After') I suspect that either:
      the copy is taking place in the first line of the function (I couldn't find a way to access the inner array without first assigning it to a variable though); or ByRef doesn't propagate into inner levels of the data structure being passed, which seems less likely to me. Could someone please point me in the right direction to get this working as intended?
      Update: the answer
      ; WRONG: ; a one-element array containing a reference to the other array Local $aOuterArray[1] = [$aInnerArray] The assumption I made about this code is wrong—it actually copies $aInnerArray into $aOuterArray, so there are now two unrelated $aInnerArray instances.  It is not possible to store arrays in other arrays by reference.
      If it is necessary to refer to a mutable array in multiple places, consider holding it in a global variable.  Where a collection of mutable arrays needs to be accessed in multiple places (as in my case), consider storing them in a global array and referring to each sub-array by index (also known as the Registry pattern).
×
×
  • Create New...