# 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 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

Live for the present,
Dream of the future,
Learn from the past.

##### Share on other sites

That was what I needed, thanks a lot!

##### 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 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```

## Create an account

Register a new account

• ### Similar Content

• By Skysnake
Hi
I am trying to set Accelerator keys from an array.
I select the KEY and CONTROL from a SQLite table, the Array looks like that generated for the Helpfile, but I can't get the CONTROLS to resolve...
I though about Assign & Eval, but not sure if that's a step in the right direction. IsDeclared shows that the \$var exists in Local Scope -1.

Local \$Main = GUICreate("Custom MsgBox", 225, 80) GUICtrlCreateLabel("Please select a button.", 10, 10) Local \$idButton_Yes = GUICtrlCreateButton("Yes", 10, 50, 65, 25) Local \$idButton_No = GUICtrlCreateButton("No", 80, 50, 65, 25) Local \$idButton_Exit = GUICtrlCreateButton("Exit", 150, 50, 65, 25) Local \$query, \$aResult, \$iRows, \$iColumns \$query = "" ;reset \$query = "Select hotkey_key, hotkey_ctrl from mytable where mykeys = 'hotkey' ; " ; ; Query \$iRval = _SQLite_GetTable2d(\$sqliteDb, \$query, \$aResult, \$iRows, \$iColumns) If \$iRval = \$SQLITE_OK Then Local \$sizeofHotkeys = UBound(\$aResult) - 1 ConsoleWrite("\$sizeofHotkeys " & \$sizeofHotkeys & @CRLF) If \$sizeofHotkeys > 0 Then Local \$main__aAccelKeys[\$sizeofHotkeys][2] For \$i = 0 To \$sizeofHotkeys - 1 \$j = \$i + 1 ; replace friendly text with code -- ! alt + Shift ^ Ctrl # Windows \$aResult[\$j][0] = StringReplace(\$aResult[\$j][0], "Alt", "!") \$aResult[\$j][0] = StringReplace(\$aResult[\$j][0], "Shift", "+") \$aResult[\$j][0] = StringReplace(\$aResult[\$j][0], "Ctrl", "^") \$main__aAccelKeys[\$i][0] = \$aResult[\$j][0] ;--- \$main__aAccelKeys[\$i][1] = \$aResult[\$j][1] ;--- Next ;~ Row|Col 0|Col 1 ;~ Row 0|F2|\$idButton_Yes ;~ Row 1|F3|\$idButton_No _DebugArrayDisplay(\$main__aAccelKeys) Local \$rv = GUISetAccelerators(\$main__aAccelKeys, \$Main) GUISetState(@SW_SHOW) ; Display the GUI.
Please note that this is a modified Helpfile example.
The Helpfile specifies (a) WinHandle and (b) last Gui created.
--> the example uses a control not a WinHandle and (b) what happens with ChildGuis? Also, the HelpFile specifies lower case, yet the examples show "{F1}" upper case?
Also, is there a way to check the result of the GuiSetAccelerator function?
Note, if I add these to lines after the FOR loop, then the F1 works, and the DebugArrayDisplays shows control 4... not it's name... So I am in the right place, but my \$vars names do not convert to their control numbers in the GUI
Next \$main__aAccelKeys[\$sizeofHotkeys - 1][0] = "{F1}" ; -- -- use the extra row for the F1 \$main__aAccelKeys[\$sizeofHotkeys - 1][1] = \$ChmHLP ;--- Skysnake
• By wolflake
Fruit
Col2 > Beef
Case "Beef" and \$aR[2] = 0
Beef1()
Case "Beef"
Beef2()
EndSwitch   On a technical note I attached the context menu to the window itself not a dummy control and I didn't use _GUICtrlMenu_TrackPopupMenu.  Instead I launched the context menu with "send shift-F10" and waited for GuiGetMsg() to give me the selection. Right click is picked up by GUIRegisterMsg WM_RBUTTONUP and Tooltips are done with GUIRegisterMsg WM_MENUSELECT. The whole thing is done with 3 functions.
I won't tell you how long it took me to figure this out but I'll say that on one of my early attempts it had two windows running at once and one was just to recieve the right click an tell the other it got it.  Suffice it to say I'm no wiz at Autoit but I really appreciate the support the community offers and I hope someone finds this useful. BTW I wrote a script to produce 1d and 2d auotit array code from excel in case you want to model your menu in excel. Here is the link.
https://www.autoitscript.com/forum/topic/139260-autoit-snippets/?do=findComment&comment=1412314
#include "ArrayToMenu.au3" ;Simple 1d array with tooltips item separator and right click. \$aM = StringSplit("Zero,One,-,Two/Two_R", ",", 3) ;make an array for the menu items \$aT = StringSplit("Zero,One,-,", ",", 3) ;make an array for the menu Tooltips \$aR = _ArrayToMenu(\$aM,\$aT) if @error then ConsoleWrite(@error & @CRLF) EndIf ConsoleWrite("R: " & \$aR[0] & " " & "C: " & \$aR[1] & " " & "Rclick: " & \$aR[2] & " " & "Item: " & \$aR[3] & @CRLF) If \$aR[0] = -1 Then ;either hit escape or clicked on another window ConsoleWrite("Esc" & @CRLF) Exit EndIf ;_ArrayDisplay(\$aR) Switch \$aR[3] Case "Zero" Zero() Case "One" One() Case "Two/Two_R" And \$aR[2] = 0 ;No Rclick Two() Case "Two/Two_R" And \$aR[2] = 1 ;Rclick Two_R() EndSwitch Func Zero() ConsoleWrite("You chose: Zero" & @CRLF) EndFunc ;==>Zero Func One() ConsoleWrite("You chose: One" & @CRLF) EndFunc ;==>One Func Two() ConsoleWrite("You chose: Two" & @CRLF) EndFunc ;==>Two Func Two_R() ConsoleWrite("You chose: Two_R" & @CRLF) EndFunc ;==>Two_R Example 2 2d array with sub-menu
#include "ArrayToMenu.au3" ;2d array with a sub-menu dim \$aM[4][2] = [["Beef", "Orange"], ["Pork", "Apple"], ["Chicken", "Grape"], ["Fruit->", ""]] ;Note you don't need a tooltip for every item but you at least need a place holder in the array dim \$aT[4][2] = [["Red Meat", "Fruit"], ["Other white meat", "Fruit"], ["White meat", "Fruit"], ["", ""]] \$aR = _ArrayToMenu(\$aM,\$aT) if @error Then ConsoleWrite(@error & @CRLF) Exit EndIf If \$aR[0] = -1 Then ConsoleWrite("Esc" & @CRLF) Exit EndIf ConsoleWrite("R: " & \$aR[0] & " " & "C: " & \$aR[1] & " " & "Rclick: " & \$aR[2] & " " & "Item: " & \$aR[3] & @CRLF) Switch \$aR[3] Case "Beef" Beef() Case "Pork" Pork() Case "Chicken" Chicken() Case "Orange" ConsoleWrite("Oranges are good for you!" & @CRLF) ConsoleWrite("Oranges" & " \$aR[0] = " & \$aR[0] & " \$aR[1] = " & \$aR[1] & @CRLF) Case "Apple" ConsoleWrite("Apples are good for you!" & @CRLF) Case "Grape" ConsoleWrite("Grapes are good for you!" & @CRLF) EndSwitch Func Beef() ConsoleWrite("Beef" & " \$aR[0] = " & \$aR[0] & " \$aR[1] = " & \$aR[1] & @CRLF) EndFunc ;==>Beef Func Pork() ConsoleWrite("Pork" & " \$aR[0] = " & \$aR[0] & " \$aR[1] = " & \$aR[1] & @CRLF) EndFunc ;==>Pork Func Chicken() ConsoleWrite("Chicken" & @CRLF) EndFunc ;==>Chicken
• 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.
The user selects the modifier key, and then uses a combination of that key with a couple others to perform tasks like
screenshot the active window, (modkey + prtscr), turn off the screen (modkey + pause / break),
open clipboard string in registry (modkey + R), change system volume (modkey + arrouw up/dn).
The following keys pressed at the same time prompt for shutdown: S+D+T
The following keys pressed at the same time prompt for restart: S+R+T
Middle mouse button click on titlebar minimizes to tray, or a left mouse button click in the icon in the tray also minimizes.
Trigger is set off by space or enter, and timeouts after 3 seconds.
Shows your external, lan, and gateway ip's, can refresh with right mouse click, and opens the default browser if the correspondent button is pressed.
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.
I made this because the existing string trigger applications didn't do it for me for a number of reasons.
I did this for me, but if someone finds it useful all the better.
• By Jeep
Here is an other UDF for string handling :
Date handling
_StringDateConvert:  convert a date from one format ("YMD", "MDY" or "DMY") to another.
_StringIsDate:  checks if a date with a given format is valid
String management
_StringCount: count of occurrences that appear in a string
_StringFormatBytesSize: formatting a dimension expressed in bytes (bytes) in MB, TB, ...)
_StringIsEndingWith: check if a string end with some characters
_StringJoinArray:  concatenate elements of an array to rebuild a string
_StringPadLeft: filling a string with characters on the left
_StringPadRight: fill a string with characters on the right
_StringRemoveFrenchAccent: remove french accent
_StringRemoveChars: deleting characters from a string
_StringStrip: eliminate characters at the begin and/or at the  end of a string
_StringTitleCaseFrench: capitalize the first letter of each word with elimination of french accents
_StringWSClean: simple replacement of "White Spaces",  remove beginning and trailing spaces and multiple spaces removal
Any comments, suggestions for improvement or constructive criticism are welcome.
Below you will find the UDF and a demo program.

JPD_String.zip
• By Ibet
Hey all,
Ending day 2 of learning AutoIt, and I'm stumped. I wrote an extremely rudimentary script simulating keystrokes for reading/copying values from one excel spreadsheet and pasting them into another spreadsheet, line by line. It works, but it doesn't use any of the Excel UDFs and was just sloppy. So, I'm trying to re-write it using some Excel UDFs to not only optimize the script, but to also learn how to use the Excel UDFs. If the answer is in a help file, please explain as I'm sometimes having problems understanding the examples in the help files.
I'm getting the error:
"C:\Users\johndoe\Desktop\AutoIt Test\AutoIt_Read spreadsheet 1 - write spreadsheet 2-version2.au3" (25) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.: MsgBox(0,"Test","Test",\$SourceEntry[1]) MsgBox(0,"Test","Test",^ ERROR >Exit code: 1 Time: 1.804 Here is the code:
#include<Array.au3> #include<Excel.au3> ;-------------------Read from Source--------------------------- Local \$oExcel_Source = _Excel_Open() Local \$sWorkbook = "C:\Users\johndoe\Desktop\AutoIt Test\AutoIt_Testing_SOURCE.xlsx" Local \$oWorkbook = _Excel_BookOpen(\$oExcel_Source,\$sWorkbook) Local \$SourceRow = 3 ;--eventually will be used to iterate through the rows, one at a time Local \$SourceEntry[5] = _Excel_RangeRead(\$oWorkbook,Default,"A"&\$SourceRow&":E"&\$SourceRow) _ArrayDisplay(\$SourceEntry, "1D Display") ;--Displays array values correctly MsgBox(0,"Test","Test",\$SourceEntry[1]) ;--Gives error, for any index in the array I want to make sure I can read the values of the array individually, before I try putting them into another document. This is because I've got to add some checks against the values already existing in the destination spreadsheet before any manipulation. I've spent the last hour or more googling that error and reading multiple posts where that error is meaning many different things, so unsure EXACTLY what the problem is. Would greatly appreciate a fix and/or explanation as well as patience with my noob-ness.
×

• Wiki

• Back

• #### Beta

• Git
• FAQ
• Our Picks
×
• Create New...