Finding 2nd and 3rd largest integers in a set

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
SOBETed
Posts: 15
Joined: 03 May 2018, 12:40

Finding 2nd and 3rd largest integers in a set

19 Mar 2019, 15:31

Hello,

I have a series of variables that contain integers var1, var2, .... and would like to find the largest, 2nd largest, 3rd largest, etc.

Thank you.
User avatar
Masonjar13
Posts: 1555
Joined: 20 Jul 2014, 10:16
Location: Не Россия
Contact:

Re: Finding 2nd and 3rd largest integers in a set

19 Mar 2019, 15:51

You would need to append the variables first, but Sort is probably what you're looking for. If you really want to keep them separate, consider making a function that will iterate through all the variables and compare. You can get the whole list of vars into a function by either using an object or variadic parameter.
OS: Windows 10 Pro | Editor: Notepad++
My Personal Function Library | Old Build - New Build
iseahound
Posts: 1444
Joined: 13 Aug 2016, 21:04
Contact:

Re: Finding 2nd and 3rd largest integers in a set

19 Mar 2019, 17:23

If you map the integer into an AHK array, then map the name of the integer as the value, you can enumerate through the array and get the variables in order.

Code: Select all

var1 := 67
var2 := 45
var3 := 77
var4 := 04

array := []

loop 4
    array[var%A_Index%] := "var" . A_Index

MsgBox % "1st largest: " array.MaxIndex() " is " array.pop()
MsgBox % "2nd largest: " array.MaxIndex() " is " array.pop()
MsgBox % "3rd largest: " array.MaxIndex() " is " array.pop()
The reason this works is because AutoHotkey will auto sort objects in alphabetical and numeric order. (Using a binary tree I think.)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 311 guests