MsgBox % Min(1) "`n" Min(1,2) "`n" Min(9,5) "`n" Min(-1,1,-2,2,-3,3) "`n" Max(-1,1,-2,2,-3,3) Min(x,x1="",x2="",x3="",x4="",x5="",x6="",x7="",x8="",x9="") { Loop IfEqual x%A_Index%,,Break Else If (x > x%A_Index%) x := x%A_Index% Return x } Max(x,x1="",x2="",x3="",x4="",x5="",x6="",x7="",x8="",x9="") { Loop IfEqual x%A_Index%,,Break Else If (x < x%A_Index%) x := x%A_Index% Return x }Handling arrays is only a little more difficult. If we pass the names to a function and the first and last index we want to process, with a handful lines of AHK code the array can be sorted, and then the first entry (Array1) is the minimum, the last entry (Array%Array0%) is the maximum, but we can also see the second largest, the median, etc. The sort is done by the built in QuickSort algorithm, so it is very fast even for huge arrays.
x0 = 10 ; Array length Loop %x0% Random x%A_Index%, 1, 10 ArraySort("s","x") ; s1, s2… <-- SORTED(x), s0 = length Loop %x0% y := y s%A_Index% "`n" MsgBox %y% ArraySort(B, A, i0="", i1="") { ; Sort array A%i0%...A%i1% --> B Local x, j ; Makes %B% global IfEqual i0,, SetEnv i0, 1 ; Default from A1... IfEqual i1,, SetEnv i1, % %A%0 ; Default: A0 tells the length IfEqual i1,, SetEnv i1, %i0% ; If A0 is not set, either x := %A%%i0% Loop % i1-i0 { j := A_Index + i0 x := x "`n" %A%%j% } Sort x, N StringSplit %B%, x, `n }We pass the names of the arrays to the function, so the output array can only be made global implicitly (declaring locals).
Another example is with an array x of 30 random elements. Choose i0 = 8, i1 = 10, that is, sort only x8, x9 and x10. The sorted array is in s, with s0 = length, s1 the smallest element and s%s0% the largest. Now, i1-i0+1 = 3 elements are sorted, therefore s0 = 3 and s1, s2 and s3 are assigned the appropriate elements of x. If there were other elements of s already defined, like s5, their values are not changed.
s5 = -9999 Loop 30 Random x%A_Index%, 10, 99 ; Assign some values to array x ArraySort("s","x",8,10) ; Sort only 3 elements of x MsgBox %s0%`n%s1%`n%s2%`n%s3%`n%s4%`n%s5%
Edit 20060309: Example call added to further explain usage