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.