sort variable array?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
AHK New user

sort variable array?

23 Aug 2017, 18:08

Hi Ahk World

I´m Trying to sort a variable ,separate by "/" but I stock , just sort the number and not the first field

Code: Select all

variable=
(
BA06=5687/
BA07E=5136/ 
BA16C=680/
BA18B=4800/
)
MsgBox %variable%
Sort variable, N D= 
MsgBox %variable%
I want this result

BA16C=680/
BA18B=4800/
BA07E=5136/
BA06=5687/

Do no this

BA06=680/
BA18B=4800/=5136/
BA16C=5687/
BA07E


As you can see the function "sort" just sort the second field (number) and not the first filed (letter & number) according the row


Thanks in advance
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: sort variable array?

23 Aug 2017, 19:00

Try this:

Code: Select all

variable=
(
BA06=5687/
BA07E=5136/
BA16C=680/
BA18B=4800/
)

MsgBox %variable%
Sort variable, F mySort
MsgBox %variable%

mySort(a,b) {
    AA := RegExReplace(a, ".*=(\d+)/", "$1")
    BB := RegExReplace(b, ".*=(\d+)/", "$1")
    Return, AA > BB ? 1 : AA < BB ? -1 : 0
}
I hope that helps.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: sort variable array?

23 Aug 2017, 19:24

This should do it. Cheers.

Code: Select all

q::
variable=
(
BA06=5687/
BA07E=5136/
BA16C=680/
BA18B=4800/
)
MsgBox %variable%
Sort variable, F SortCustom
MsgBox %variable%
return

SortCustom(vTextA, vTextB, vOffset)
{
	vNumA := StrSplit(vTextA,["=","/"]).2
	vNumB := StrSplit(vTextB,["=","/"]).2
	;MsgBox, % vNumA " " vNumB
	return vNumA > vNumB ? 1 : vNumA < vNumB ? -1 : -vOffset
}
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: sort variable array?

23 Aug 2017, 19:31

@ jeeswg: Nice one! No RegEx! :thumbup:

May I ask you a question: What on earth is this offset parameter for? I read the docs minimum 5 times, I just don't get it. I noticed you use it often in custom sort functions, so maybe you can offer an explanation?
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: sort variable array?

23 Aug 2017, 19:51

Sure, it's about stable v. unstable sort. If a sort function interprets two items as being the same, do you keep them those items left/right relative to the original positions in the list, or do you allow the items to be shuffled.

Sometimes, e.g. if it's a case-sensitive string sort, you would get the same results whether using stable or unstable sort. Unstable sort has the advantage of being faster.

The classic surprise you get if using an unstable sort, is that in a list sorted as case insensitive, certain words lose their position relative to other words. E.g. 'a,a,A,a,a' to 'a,A,a,a,a'. I think that most people would agree it's preferable to maintain the original order for items regarded as equal by the sort function, and would expect that to be the default behaviour. Also realistically, the saving in time is probably minuscule, and if you wanted to use unstable sort instead, that could be an option.

I was quite surprised that AutoHotkey doesn't have a 'stable sort' option, and I think that it ought to be implemented. AutoHotkey is supposed to be newb friendly, and 'stable sort' is one of the things a newb would expect to be the default behaviour.

[EDIT:] I just remembered that when I first came across this issue, it was in AutoHotkey, I was trying to understand why my lists were getting messed up. And eventually I discovered the term 'stable sort'. Different definitions of the Mod function is another thing that causes confusion. Because of the stable sort issue, I, generally speaking, only use the Sort command with a custom Sort function.

Code: Select all

q::
vText := " ;continuation section
(
a
b
c
d
)"
;positions: a 1, b 3, c 5, d 7
Sort, vText, F SortReverse
return

w::
;unstable sort
;AHK does not maintain order
vText := "a,a,A,a,a"
Sort, vText, D,
MsgBox, % vText ;a,A,a,a,a

;stable sort
;if items are equal, they maintain their relative position
vText := "1,1.0,1.00"
Sort, vText, D, F SortStable
MsgBox, % vText ;1,1.0,1.00

;unstable sort
;if items are equal, they may be shuffled
vText := "1,1.0,1.00" ;1.0,1.00,1
Sort, vText, D, F SortUnstable
MsgBox, % vText
return

SortReverse(vTextA, vTextB, vOffset)
{
	MsgBox, % vTextA " " vTextB " " vOffset
	return vOffset
}
SortStable(vTextA, vTextB, vOffset)
{
	return vNumA > vNumB ? 1 : vNumA < vNumB ? -1 : -vOffset
}
SortUnstable(vTextA, vTextB)
{
	return vNumA > vNumB ? 1 : vNumA < vNumB ? -1 : 0
}

;==================================================
@wolf_II: Btw re. your approach I was thinking, 'oh yeah RegEx, saves on creating an object'. Although I had StrSplit in mind because I thought it might be easier to understand/adapt to other tasks.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
AHK New user

Re: sort variable array?

23 Aug 2017, 19:52

Thanks to you guys wolf_II & jeeswg

You fix the problem
But if the variable is like this

variable=BA06=5687/BA07E=5136/BA16C=680/BA18B=4800/

just 1 line

Thanks in advance
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: sort variable array?

23 Aug 2017, 20:21

@ jeeswg : Thank you. I did not realize the unstable nature of the default AHK Sort command.
I have also applied my newly found knowledge to the following variation with no RegEx and no Array.

Code: Select all

variable=
(
BA06=5687/
BA07E=5136/
BA16C=680/
BA18B=4800/
)

MsgBox %variable%
Sort variable, F mySort
MsgBox %variable%



;-------------------------------------------------------------------------------
mySort(a, b, offset) { ; sort by number between "=" and "/"
;-------------------------------------------------------------------------------
    AA := SubStr(a, Pos := InStr(a, "=") + 1, InStr(a, "/") - Pos)
    BB := SubStr(b, Pos := InStr(b, "=") + 1, InStr(b, "/") - Pos)
    Return, AA > BB ? 1 : AA < BB ? -1 : -offset
}
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: sort variable array?

23 Aug 2017, 20:33

@wolf_II: Thanks for your response. Interesting. It was because of things like this that I started writing my documentation extension, which tries to collect all the most important but non-obvious things to know about AutoHotkey, I just checked and 'unstable sort' is mentioned. Any other suggestions for inclusions are welcome there.

jeeswg's documentation extension tutorial - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=33596
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: jeves, mikeyww and 285 guests