# 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 roeselpi
hello again,
it has been a long time since i have been here and a long time since i last used autoit. ever so often when the time allows me to, then i follow up on an idea that i had a long time ago. i have done all the work on paper but now it is up to writing it in autoit and i keep stumbling over many little issues here and there. sometimes after a few days i will try again and get a step further but sometimes it just will not help no matter how long i try and think about a solution. for most of you it will be the basics but for me it is not all that easy, but at least i give it a try.
here is my code:
#include <MsgBoxConstants.au3> #include <StringConstants.au3> #include <Array.au3> #include <String.au3> ; ; PART 1: define replacements and check with msgbox ; Global \$y, \$z \$y = "Yes" \$z = "No" MsgBox(0,"replacements", \$y & @CRLF & \$z) ;the replacements in a message box ; ; PART 2: set the texts and check via console and msgbox ; Global \$my1string = "abab" ;the first specified text MsgBox(0,"my1string", \$my1string) ;the message box to output the first specified text Global \$my2string = "icic" ;the second specified text MsgBox(0,"my2string", \$my2string) ;the message box to output the second specified text ; ; PART 3: transform the strings to individual arrays ; \$my1array = StringSplit(\$my1string, "") \$my1array[0] = "" _ArrayDelete(\$my1array, 0) _ArrayDisplay(\$my1array, "my1array") ;the display of the first specified array \$my2array = StringSplit(\$my2string, "") \$my2array[0] = "" _ArrayDelete(\$my2array, 0) _ArrayDisplay(\$my2array, "my2array") ;the display of the first specified array ; ; PART 4: create an empty array for filling ; Global \$OutputArray[4] \$OutputArray[0] = "" _ArrayDisplay(\$OutputArray, "OutputArray") ;the display of the first specified array ; ; PART 5: compare & fill empty OutputArray with data after evaluation ; Global \$i, \$j, \$k For \$i = 0 to UBound(\$my1array) -1 For \$j = 0 to UBound(\$my2array) -1 For \$k = 0 to UBound(\$OutputArray) -1 If \$my1array[\$i] = "a" And \$my2array[\$j] = "i" Then \$OutputArray[\$k] = \$y Else \$OutputArray[\$k] = \$z EndIf Next Next Next _ArrayDisplay(\$OutputArray, "OutputArray") ;the display of the Newly filled Array In "Part 2" i make a string that is converted to an array in "Part 3" ... Now, I know that "a" and "i" are always in the exact same spot in both arrays and so i wanted to compare this and make a further array to document my findings by saying "yes" or "no" ... however my new array keeps saying just "no" allthough i can clearly see and know that it should say:
yes no yes no my guess is that there is something wrong within my for-loops and that the counting is somehow "off" i guess that when the first for-loop is finished it reaches the second whilst the second for-loop is checking the first which would explain why it always says "no" instead of seeing the obvious.
so my question would be: what is wrong with my for-loop? or where am i making an error that ultimately gives me the wrong results?
help is much appreciated.
kind regards
roeselpi

PS: sorry for my not so great english spelling ... stupid german sitting here trying out intermediate english skills.
• By Jangal
Hello friends
This app is slow
How to increase its speed?

#include <Array.au3> #include <StringConstants.au3> #include <File.au3> #include <String.au3> Global \$aWord[][2]  = [[1, "google"],[2,"hello"]]

Global \$sFileName = @ScriptDir & "\1.txt" ; 2MB Text File Local \$sFileRead = FileRead(\$sFileName) Local \$res = StringRegExp(\$sFileRead, "(*UCP)\b[\pL\d]{2,}", 3) _ArrayDisplay(\$res)   for \$sWord in \$res     \$iIndex = _ArraySearch(\$aWord, \$sWord, 0, 0, 0, 0, 1, 2)     ;MsgBox(0,0,\$iIndex)     if \$iIndex == -1 Then         Local \$aFill = [[0,\$sWord]]         _ArrayAdd(\$aWord,\$aFill)        ;      Else         \$aWord[\$iIndex][0] +=1     EndIf   Next _ArrayDisplay(\$aWord)

1.txt

• Hi dear friends!, i'm sorry for creating a new thread (a new problem), i have over than 9 lists that i want to combine them to be this (in this example, there are 3 test files):

I've written a little code for splitting main information, but i really confused how to make results as "Output.txt", here is that code:

\$sRegex_1 = StringRegExp(FileRead("1.txt"), '(?s:(?<=\=\=\r\n)(.*?)(?=\r\n\=\=))', 3) \$sRegex_2 = StringRegExp(FileRead("2.txt"), '(?s:(?<=\=\=\r\n)(.*?)(?=\r\n\=\=))', 3) \$sRegex_3 = StringRegExp(FileRead("3.txt"), '(?s:(?<=\=\=\r\n)(.*?)(?=\r\n\=\=))', 3) For \$i = 0 To UBound(\$sRegex_1) - 1 ConsoleWrite(\$sRegex_1[\$i] & @CRLF) For \$j = 0 To UBound(\$sRegex_2) - 1 ConsoleWrite(\$sRegex_2[\$j] & @CRLF) For \$k = 0 To UBound(\$sRegex_3) - 1 ConsoleWrite(\$sRegex_3[\$k] & @CRLF) Next Next Next

• hello evrybody
here is an example about how to split your texts using a delimiter with the ability to select how much of delimiters shows in each colum  with \$i_number
e.g
you have a long text and you want to split it in an array
that evry colum have a number (n) of lines
i made a function that do that for you
just call it with a three params
\$s_text
\$i_number
the number that you want to put in each col
\$s_siparator
the siparator
default is "|"
here is the function with example
i hope that it will be useful for you

****

#include <Array.au3> \$s_txt = "some text1some text2|some text3|some text4|some text5|some text6" \$array = splitText(\$s_txt, 2) _ArrayDisplay(\$array) Func splitText(\$s_text, \$i_number, \$s_siparator = "|") Local \$a_TXT = StringSplit(\$s_text, \$s_siparator) Local \$a_Return[\$a_TXT[0] + 1] If (\$a_TXT[0] <= \$i_number) Or (\$i_number <= 0) Then ReDim \$a_Return[2] \$a_Return[0] = 1 \$a_Return[1] = \$s_text Return \$a_Return EndIf Local \$i_Processed = 1, \$i_arrayProcessed = 1 Do For \$i = \$i_Processed To (\$i_Processed + \$i_number) - 1 If (\$a_TXT[0] < \$i) Then ExitLoop If Not (\$a_Return[\$i_arrayProcessed]) Then \$a_Return[\$i_arrayProcessed] = \$a_TXT[\$i] Else \$a_Return[\$i_arrayProcessed] &= \$s_siparator & \$a_TXT[\$i] EndIf \$i_Processed += 1 Next \$i_arrayProcessed += 1 Until (\$a_TXT[0] < \$i_Processed) ReDim \$a_Return[\$i_arrayProcessed] \$a_Return[0] = \$i_arrayProcessed - 1 Return \$a_Return EndFunc ;==>splitText
accept my greetings
thanks to
@Dan_555
for his notes

• good morning
this is the first post here in the autoit forums
i hope that you can help me in my problem
i have a JSON encoded
it a map of my forums
where i want to make a treeview that have the same type of map
e.g
a system (as category)
windows (as sub category)
software (as an child item in the windows category)
.....
i don't know how to do that
so, i know that i can do that using the json functions
but i need your help about how we can do it as the type that i told you
by the way i need to put the sub info for each item in an array that give me the ability to manage my items
e.g
message cound ...
you just give me a small example and i can continue.
am sorry if this against the rules of the forum.
but i realy searched a lot but i couldn't
i hope some one give me the way.
thank you very much in advance

here is the link of json forum
https://www.autoitscript.com/forum/topic/148114-a-non-strict-json-udf-jsmn/
and here is my encoded json file

×

• Wiki

• Back

• #### Beta

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