Jump to content
Sign in to follow this  
feat

Sorting Variables

Recommended Posts

feat

How can I make AutoIT sorting variables by value? Only numbers. I have 10 variables and I want AutoIT to sort them from the highest to the lowest number :) Would be nice if someone could help me :lmao:

best regards,

feat

Share this post


Link to post
Share on other sites
Authenticity

_ArraySort(), include Array.au3.

In the case of much larger array (bigger than 30 members) it's much faster to use quicksort method. You can google it and make your own.

Share this post


Link to post
Share on other sites
feat

Thank you for the very quick answer, I am very new to AutoIT and programming in general, could you maybe give me a short example? I read the help file already before and I dont really get it =\ After they are sorted I have to sort them again in 2 groups that means could it be possible if my variables are $p1 $p2 $p3 and they get sorted from max to min they are like this $p2 $p3 $p1 and then they get saved as another variable like the highest is $h1 and the second $h2 and so on.

best regards,

feat

Share this post


Link to post
Share on other sites
Authenticity

I think this is what you've requested but it might be bad example because it's better to use only array in this case:

#include <Array.au3>
Opt('MustDeclareVars', 1)

Dim $Arr[5], $s0 = 5, $s1 = 22, $s2 = 19, $s3 = 3, $s4 = 31

$Arr[0] = $s0
$Arr[1] = $s1
$Arr[2] = $s2
$Arr[3] = $s3
$Arr[4] = $s4

_ArraySort($Arr)

$s0 = $Arr[0]
$s1 = $Arr[1]
$s2 = $Arr[2]
$s3 = $Arr[3]
$s4 = $Arr[4]

Dim $txt = $s0 & ', ' & $s1 & ', ' & $s2 & ', ' & $s3 & ', ' & $s4

MsgBox(0x40, 'Sorted ascending', $txt)oÝ÷ Ø  趫{¦¦W¦¹È[^¬jëh×6#include <Array.au3>
Opt('MustDeclareVars', 1)

Dim $Arr[300], $i, $j, $txt = ''

$j = 1

For $i = 299 To 0 Step -1
    $Arr[$i] = $j
    $j += 1
Next

_ArrayDisplay($Arr, 'Array: sorted ascending')

QuickSort($Arr, 0, UBound($Arr)-1)

_ArrayDisplay($Arr, 'Array: sorted ascending')


Func QuickSort(ByRef $Array, $lo = 0, $hi = 299)
    Local $i = $lo, $j = $hi, $h, $x = $Array[($lo+$hi)/2]
    
    While $i <= $j
        While $Array[$i] < $x
            $i += 1
        WEnd
        
        While $Array[$j] > $x
            $j -= 1
        WEnd
        
        If $i <= $j Then
            $h = $Array[$i]
            $Array[$i] = $Array[$j]
            $Array[$j] = $h
            $i += 1
            $j -= 1
        EndIf
    WEnd
    
    If $lo < $j Then QuickSort($Array, $lo, $j)
    If $i < $hi Then QuickSort($Array, $i, $hi)
    
EndFunc

Share this post


Link to post
Share on other sites
feat

Nice thank you so much, I'll work through this, while you wrote this I finally unsterstood how arrays work, thank you really much, this will help me. Thank you for your time, really^^

best regards,

feat

Edited by feat

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  

×