Jump to content

Sorting Variables


feat
 Share

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...