Page 1 of 1

Sort doesn't work with descend order?

Posted: 27 Mar 2024, 00:15
by songdg
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
}

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

Posted: 27 Mar 2024, 02:42
by boiler
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
}

Re: Sort doesn't work with descend order?

Posted: 27 Mar 2024, 22:46
by songdg
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: