Sort Variable name by their decimal value Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
labrint
Posts: 379
Joined: 14 Jun 2017, 05:06
Location: Malta

Sort Variable name by their decimal value

Post by labrint » 04 Dec 2021, 15:41

Need code that gives me the following output:

Option1NetCost,Option3NetCost,Option2NetCost (ascending order)

Tried using min max to find what should go first but keep in mind two options may have the same value hence its confusing.

Code: Select all

Option1NetCost = 262.11 
Option2NetCost = 11211.3
Option3NetCost = 262.23
Please help. The array sorting I found only works for integers, and the decimal sorting I found did not give me variable names.

User avatar
labrint
Posts: 379
Joined: 14 Jun 2017, 05:06
Location: Malta

Re: Sort Variable name by their decimal value

Post by labrint » 04 Dec 2021, 15:49

labrint wrote:
04 Dec 2021, 15:41
Need code that gives me the following output:

Option1NetCost,Option3NetCost,Option2NetCost (ascending order)

Tried using min max to find what should go first but keep in mind two options may have the same value hence its confusing.

Code: Select all

Option1NetCost = 262.11 
Option2NetCost = 11211.3
Option3NetCost = 262.23
Please help. The array sorting I found only works for integers, and the decimal sorting I found did not give me variable names.
This only works for whole numbers
viewtopic.php?t=37403

This also works for whole numbers but fails with decimals

Code: Select all

array := {30: "thirty", 10: "ten", 20: "twenty"}
For key, value in array
    MsgBox %key% = %value%

User avatar
labrint
Posts: 379
Joined: 14 Jun 2017, 05:06
Location: Malta

Re: Sort Variable name by their decimal value

Post by labrint » 04 Dec 2021, 16:00

labrint wrote:
04 Dec 2021, 15:49
labrint wrote:
04 Dec 2021, 15:41
Need code that gives me the following output:

Option1NetCost,Option3NetCost,Option2NetCost (ascending order)

Tried using min max to find what should go first but keep in mind two options may have the same value hence its confusing.

Code: Select all

Option1NetCost = 262.11 
Option2NetCost = 11211.3
Option3NetCost = 262.23
Please help. The array sorting I found only works for integers, and the decimal sorting I found did not give me variable names.
This only works for whole numbers
viewtopic.php?t=37403

This also works for whole numbers but fails with decimals

Code: Select all

array := {30: "thirty", 10: "ten", 20: "twenty"}
For key, value in array
    MsgBox %key% = %value%

This code does exactly what I need but I do not wish to use a GUI

Code: Select all

start:
Gui, Add, ListView, r20 w200, 1|2
data =
(
Option1NetCost,262.11
Option2NetCost,11211.3
Option3NetCost,262.23
)
 
Loop, parse, data, `n
{
  stringsplit, row, A_LoopField, `,
  LV_Add(row, row1, row2)
}
LV_ModifyCol()  ; Auto-size columns
Gui, Show
LV_ModifyCol(2, "sort Float") ; sort by second column numerically
return
 
GuiClose:
ExitApp

User avatar
flyingDman
Posts: 2817
Joined: 29 Sep 2013, 19:01

Re: Sort Variable name by their decimal value  Topic is solved

Post by flyingDman » 04 Dec 2021, 16:04

try:

Code: Select all

var = 
(
Option1NetCost = 262.11 
Option2NetCost = 11211.3
Option3NetCost = 262.23
)
sort, var, NP18
msgbox % var
14.3 & 1.3.7

User avatar
labrint
Posts: 379
Joined: 14 Jun 2017, 05:06
Location: Malta

Re: Sort Variable name by their decimal value

Post by labrint » 04 Dec 2021, 16:42

flyingDman wrote:
04 Dec 2021, 16:04
try:

Code: Select all

var = 
(
Option1NetCost = 262.11 
Option2NetCost = 11211.3
Option3NetCost = 262.23
)
sort, var, NP18
msgbox % var
Remarkable. Thanks @flyingDman

sofista
Posts: 650
Joined: 24 Feb 2020, 13:59
Location: Buenos Aires

Re: Sort Variable name by their decimal value

Post by sofista » 04 Dec 2021, 17:14

A regex in a custom sort function alternative, it does not require the numbers to have a fixed initial position.

Code: Select all

var = 
(
Option1NetCost = 262.11 
Option2NetCost = 11211.3
Option3NetCost = 262.23
)

Sort var, F SortCustom
MsgBox, % var
return

SortCustom(TextA, TextB, xOffset)
{
	NumA := RegExReplace(TextA, ".+ = (.+)$", "$1")
	NumB := RegExReplace(TextB, ".+ = (.+)$", "$1")
	;MsgBox, % NumA " " NumB
	return NumA > NumB ? 1 : NumA < NumB ? -1 : -xOffset
}

/* Output:

Option1NetCost = 262.11
Option3NetCost = 262.23
Option2NetCost = 11211.3
 */

User avatar
flyingDman
Posts: 2817
Joined: 29 Sep 2013, 19:01

Re: Sort Variable name by their decimal value

Post by flyingDman » 04 Dec 2021, 18:04

Or:

Code: Select all

var = 
(
anylength = 262.11 
anylengthXXXX = 11211.3
anylengthXX = 262.23
)

for a,b in strsplit(var,"`n","`r")
	lst .= strsplit(b, " = ").2 " = " strsplit(b, " = ").1 "`n"
sort, lst, N
for a,b in strsplit(trim(lst,"`n"),"`n","`r")
	Nlst .= strsplit(b, " = ").2 " = " strsplit(b, " = ").1 "`n"
msgbox % Nlst
14.3 & 1.3.7

User avatar
labrint
Posts: 379
Joined: 14 Jun 2017, 05:06
Location: Malta

Re: Sort Variable name by their decimal value

Post by labrint » 05 Dec 2021, 01:59

Thanks folks was wondering what happens when the number doesn't have a fixed position.

User avatar
AlphaBravo
Posts: 586
Joined: 29 Sep 2013, 22:59

Re: Sort Variable name by their decimal value

Post by AlphaBravo » 05 Dec 2021, 10:41

another way

Code: Select all

var = 
(
anylength = 262.11 
anylengthXXXX = 11211.3
anylengthXX = 262.23
)
Sort, Var, F MySort
MsgBox % var
return
MySort(a1, a2){
	a1 := StrSplit(a1, "=").2
	a2 := StrSplit(a2, "=").2
    return a1 > a2 ? 1 : a1 < a2 ? -1 : 0
}

User avatar
labrint
Posts: 379
Joined: 14 Jun 2017, 05:06
Location: Malta

Re: Sort Variable name by their decimal value

Post by labrint » 06 Dec 2021, 02:57

Nicely done @AlphaBravo

Post Reply

Return to “Ask for Help (v1)”