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 nacerbaaziz
      hello evrybody
      here is an example about how to split your texts with a number and siparator
      e.g
      you have a long text and you want to split it in an array
      that evry colum have a number of lines
      i made a function that do that for you
      just call it with a three params
      $s_text
      your 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
       
      ****
       
      $s_txt = "some text1some text2|some text3|some text4|some text5|some text6|some text7" $array = splitText($s_txt, 2) For $i = 1 To $array[0] MsgBox(64, $i, $array[$i]) Next 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 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 While 1 For $i = $i_Processed To $i_Processed + $i_number - 1 If ($a_TXT[0] < $i) Then ExitLoop If $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 If ($a_TXT[0] < $i_Processed) Then ExitLoop WEnd ReDim $a_Return[$i_arrayProcessed] $a_Return[0] = $i_arrayProcessed - 1 Return $a_Return EndFunc ;==>splitText  
      accept my greetings
    • By MesterPerfect
      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
      can post thread
      can reply
      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
       
      { "tree_map": { "0": [ 1, 5, 6, 7 ], "1": [ 2 ], "2": [ 4 ], "5": [ 3 ], "6": [ 8 ], "8": [ 9, 10 ] }, "nodes": [ { "breadcrumbs": [], "description": "", "display_in_list": true, "display_order": 1, "node_id": 1, "node_name": null, "node_type_id": "Category", "parent_node_id": 0, "title": "Main category", "type_data": {} }, { "breadcrumbs": [ { "node_id": 1, "title": "Main category", "node_type_id": "Category" } ], "description": "", "display_in_list": true, "display_order": 1, "node_id": 2, "node_name": null, "node_type_id": "Forum", "parent_node_id": 1, "title": "Main forum", "type_data": { "allow_poll": true, "allow_posting": true, "can_create_thread": true, "can_upload_attachment": true, "discussion_count": 0, "last_post_date": 0, "last_post_id": 0, "last_post_username": "", "last_thread_id": 0, "last_thread_prefix_id": 0, "last_thread_title": "", "message_count": 0, "min_tags": 0, "require_prefix": false } }, { "breadcrumbs": [ { "node_id": 1, "title": "Main category", "node_type_id": "Category" }, { "node_id": 2, "title": "Main forum", "node_type_id": "Forum" } ], "description": "", "display_in_list": true, "display_order": 1, "node_id": 4, "node_name": null, "node_type_id": "Forum", "parent_node_id": 2, "title": "my forums1", "type_data": { "allow_poll": true, "allow_posting": true, "can_create_thread": true, "can_upload_attachment": true, "discussion_count": 0, "last_post_date": 0, "last_post_id": 0, "last_post_username": "", "last_thread_id": 0, "last_thread_prefix_id": 0, "last_thread_title": "", "message_count": 0, "min_tags": 0, "require_prefix": false } }, { "breadcrumbs": [], "description": "", "display_in_list": true, "display_order": 2, "node_id": 5, "node_name": null, "node_type_id": "Category", "parent_node_id": 0, "title": "Perfect", "type_data": {} }, { "breadcrumbs": [ { "node_id": 5, "title": "Perfect", "node_type_id": "Category" } ], "description": "", "display_in_list": true, "display_order": 2, "node_id": 3, "node_name": null, "node_type_id": "Forum", "parent_node_id": 5, "title": "ahmed", "type_data": { "allow_poll": true, "allow_posting": true, "can_create_thread": true, "can_upload_attachment": true, "discussion_count": 0, "last_post_date": 0, "last_post_id": 0, "last_post_username": "", "last_thread_id": 0, "last_thread_prefix_id": 0, "last_thread_title": "", "message_count": 0, "min_tags": 0, "require_prefix": false } }, { "breadcrumbs": [], "description": "", "display_in_list": true, "display_order": 3, "node_id": 6, "node_name": null, "node_type_id": "Forum", "parent_node_id": 0, "title": "autoit", "type_data": { "allow_poll": true, "allow_posting": true, "can_create_thread": true, "can_upload_attachment": true, "discussion_count": 0, "last_post_date": 0, "last_post_id": 0, "last_post_username": "", "last_thread_id": 0, "last_thread_prefix_id": 0, "last_thread_title": "", "message_count": 0, "min_tags": 0, "require_prefix": false } }, { "breadcrumbs": [ { "node_id": 6, "title": "autoit", "node_type_id": "Forum" } ], "description": "", "display_in_list": true, "display_order": 3, "node_id": 8, "node_name": null, "node_type_id": "Forum", "parent_node_id": 6, "title": "examples", "type_data": { "allow_poll": true, "allow_posting": true, "can_create_thread": true, "can_upload_attachment": true, "discussion_count": 0, "last_post_date": 0, "last_post_id": 0, "last_post_username": "", "last_thread_id": 0, "last_thread_prefix_id": 0, "last_thread_title": "", "message_count": 0, "min_tags": 0, "require_prefix": false } }, { "breadcrumbs": [ { "node_id": 6, "title": "autoit", "node_type_id": "Forum" }, { "node_id": 8, "title": "examples", "node_type_id": "Forum" } ], "description": "", "display_in_list": true, "display_order": 3, "node_id": 9, "node_name": null, "node_type_id": "Forum", "parent_node_id": 8, "title": "GUI", "type_data": { "allow_poll": true, "allow_posting": true, "can_create_thread": true, "can_upload_attachment": true, "discussion_count": 0, "last_post_date": 0, "last_post_id": 0, "last_post_username": "", "last_thread_id": 0, "last_thread_prefix_id": 0, "last_thread_title": "", "message_count": 0, "min_tags": 0, "require_prefix": false } }, { "breadcrumbs": [ { "node_id": 6, "title": "autoit", "node_type_id": "Forum" }, { "node_id": 8, "title": "examples", "node_type_id": "Forum" } ], "description": "", "display_in_list": true, "display_order": 31, "node_id": 10, "node_name": null, "node_type_id": "Forum", "parent_node_id": 8, "title": "windowEX", "type_data": { "allow_poll": true, "allow_posting": true, "can_create_thread": true, "can_upload_attachment": true, "discussion_count": 0, "last_post_date": 0, "last_post_id": 0, "last_post_username": "", "last_thread_id": 0, "last_thread_prefix_id": 0, "last_thread_title": "", "message_count": 0, "min_tags": 0, "require_prefix": false } }, { "breadcrumbs": [], "description": "", "display_in_list": true, "display_order": 4, "node_id": 7, "node_name": null, "node_type_id": "Category", "parent_node_id": 0, "title": "vbs", "type_data": {} } ] }  
    • By cruisepandey
      Hi, 
      I have a string like this : 
      Global $Msga = "urrent directory is /send.  (Submission of file with log number 29381077284 is confirmed)";
      I want to extract the number 29381077284  from the string. I did StringSplit to split based on "(" and then use space to reach there, But it's not a good choice. 
      Can anyone help me with regular expression to find the number from String using AutoIT. TIA
    • By DannyJ
      I have this example string:
       
      2020-06-08 09:23:33 : abcdefghifjklm How to trim just the right part? Or how to trim this text right to left?
      I want to trim this string right to left until ' : '.
      In ohter words: I need the 'abcdefghifklm' text.
      I have tried StringTrimRight, but that requires 
      The number of characters to trim. Unfortunately StringSplit trims the left part of the strings.
       
       
    • By careca
      This is my 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.
      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.
×
×
  • Create New...