Sort doesn't work with descend order? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
songdg
Posts: 575
Joined: 04 Oct 2017, 20:04

Sort doesn't work with descend order?

27 Mar 2024, 00:15

Sort doesn't work with descend order? I want to sort the lines with the number at the end, and specify "NR" in Options, but it always in ascend order.

Code: Select all

Contents := "
(
	wwwww=3
	fffff=8
	ggggg=2
)"
MsgBox Sort(Contents, "NR", NumSort)

NumSort(a1, a2, *)
{
	a1 :=Number(StrSplit(a1, "=")[2])
	a2 := Number(StrSplit(a2, "=")[2])
    return a1 > a2 ? 1 : a1 < a2 ? -1 : 0
}
User avatar
boiler
Posts: 16978
Joined: 21 Dec 2014, 02:44

Re: Sort doesn't work with descend order?  Topic is solved

27 Mar 2024, 02:42

You have a custom function to determine how things are sorted. Specifying some options has no effect. There is no way for AHK to know how to change the results of your custom function just because you specified a couple options. To change the sort order, you have to change the function itself:

Code: Select all

Contents := "
(
	wwwww=3
	fffff=8
	ggggg=2
)"
MsgBox Sort(Contents,, NumSort)

NumSort(a1, a2, *)
{
	a1 :=Number(StrSplit(a1, "=")[2])
	a2 := Number(StrSplit(a2, "=")[2])
    return a1 < a2 ? 1 : a1 > a2 ? -1 : 0
}
songdg
Posts: 575
Joined: 04 Oct 2017, 20:04

Re: Sort doesn't work with descend order?

27 Mar 2024, 22:46

boiler wrote:
27 Mar 2024, 02:42
You have a custom function to determine how things are sorted. Specifying some options has no effect. There is no way for AHK to know how to change the results of your custom function just because you specified a couple options. To change the sort order, you have to change the function itself:

Code: Select all

Contents := "
(
	wwwww=3
	fffff=8
	ggggg=2
)"
MsgBox Sort(Contents,, NumSort)

NumSort(a1, a2, *)
{
	a1 :=Number(StrSplit(a1, "=")[2])
	a2 := Number(StrSplit(a2, "=")[2])
    return a1 < a2 ? 1 : a1 > a2 ? -1 : 0
}
Thank you very much, much appreciated for your help :superhappy:

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: alawsareps and 144 guests